Rory Baffle
Rory Baffle

Reputation: 155

How can I avoid using the type name type

I have a declaration in my base class:

template<class PROTOCOL>
static Packet* serialize(uint packetId, QVariantHash data = QVariantHash());

Then when I inherit from the base class, I can use the static method like so:

GameProtocol::serialize<GameProtocol>(0); // This works fine

My question is, what do I have to change in order to make the call GameProtocol::serialize(0) work (ie. without the template declaration).

I would like to keep the method static, as it simplifies other areas of the base class. I understand this makes it difficult as static methods cannot be overridden in C++, but there surely must be a way using template magic.

Upvotes: 1

Views: 108

Answers (2)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154015

It seems GameProtocol happens to be your derived class: simply add a static method serialize() which forwards to the appropriate version of the base class:

class GameProtocol: public Protocol {
    // ...
public:
    static Packet* serialize(uint id,
        QVariantHash h = QVariantHash()) {
        return Protocol::serialize<GameProtocol>(id, h);
    }
    // ...
};

Upvotes: 1

Faker
Faker

Reputation: 61

Simply write another function that wrap the template function in your GameProtocol class:

static Packet* serialize(int packetId, QVariantHash data = QVariantHash()) {
    return serialize<GameProtocol>( packetId, data );
}

now you can get rid of the template:

GameProtocol::serialize(0);

Upvotes: 0

Related Questions