Ed James
Ed James

Reputation: 10607

TypeDef as an overridable class feature

If I have a class that contains a number of typedef'd variables, like so:

class X {

typedef token TokenType;

bool doStuff()
{
TokenType data;
fillData(&data);
return true;
}

};

Is there any way to override the typedef for TokenType in a derived class?

N.B. This is NOT a good place to use templates (This is already a templated class and any changes are likely to result in [EDIT: infinite] recursive definitions [class X < class Y = class X < class Y . . .> > etc.].)

Upvotes: 4

Views: 4414

Answers (3)

Aaron Saarela
Aaron Saarela

Reputation: 4036

Short answer: No, you cannot override typedefs.

Long answer: A typedef is basically an alias or synonym for another type. They don't define new data types. They simply provide a way to give a type a new name.

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 545628

Typedefs are resolved at compile time – making them overridable would be meaningless, since overriding is a feature of runtime polymorphism.

Simply redeclaring the typedef will work – though I'm not sure why you think templates would be a bad idea here – recursive templates are actually feasible.

Upvotes: 3

Pontus Gagge
Pontus Gagge

Reputation: 17258

What you can do is shadow, but not override. That is: you can define a derived class Y with its own typedefs for TokenType, but that will only come into play if somebody references Y::TokenType directly or via an object statically typed as Y. Any code that references X::TokenType statically will do so even for objects of type Y.

Upvotes: 8

Related Questions