Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeDef as an overridable class feature

Tags:

c++

typedef

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.].)

like image 244
Ed James Avatar asked Dec 23 '22 12:12

Ed James


2 Answers

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.

like image 114
Pontus Gagge Avatar answered Feb 24 '23 08:02

Pontus Gagge


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.

like image 20
Konrad Rudolph Avatar answered Feb 24 '23 10:02

Konrad Rudolph