TypeDef as a class function

If I have a class containing several typedef 'd variables, for example:

 class X { typedef token TokenType; bool doStuff() { TokenType data; fillData(&data); return true; } }; 

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

NB This is not a suitable place to use templates (it is already a template class, and any changes can lead to recursive definitions [EDIT: infin] [ class X < class Y = class X < class Y . . .> > .... class X < class Y = class X < class Y . . .> > Etc.] .)

+4
source share
3 answers

What you can do is shadow but not override. That is, you can define a derived class Y with your typedefs for TokenType, but this will only come into play if someone refers to Y :: TokenType directly or through an object statically entered as Y. Any code that refers to X :: TokenType will statically do this even for objects of type Y.

+8
source

Typedefs are resolved at compile time, which makes them redefinable, it would be pointless because redefinition is a function of run-time polymorphism.

Just reusing typedef will work, although I'm not sure why you think templates would be a bad idea here. Recursive patterns are really possible.

+3
source

Short answer: No, you cannot override typedefs.

Long answer: typedef is basically an alias or synonym for another type. They do not define new data types. They simply provide a way to give the type a new name.

0
source

All Articles