Mister Zimbu
Member
Too used to Java (which does OO *right*
... I can't get this working:
There are other classes that also inherit from parsable.
Later on, I have a vector of children of Parsables (no Parsables themselves). I want to iterate through the list, and do some checks on their properties. From those checks I can determine whether it's a Token or not. One of the things I want to do is get the id of the token (through getId).
The problem is, how do I convert the Parsable to a Token and maintain all the internal data? In Java it's easy, i can just say:
Token t = parsableList.at(i);
Or in the worst case scenario I can typecast it there:
Token t = (Token)(parsableList.at(i));
No luck in C++ though. Both give me errors.
I tried giving a getId() function to Parsable, but when I do that and call it. the Token's getId function won't override it.
Any suggestions?
Code:
class Parsable {
public:
virtual void parse();
};
class Token: public Parsable {
public:
void parse();
Token( string id );
string getId();
private:
string id;
};
There are other classes that also inherit from parsable.
Later on, I have a vector of children of Parsables (no Parsables themselves). I want to iterate through the list, and do some checks on their properties. From those checks I can determine whether it's a Token or not. One of the things I want to do is get the id of the token (through getId).
The problem is, how do I convert the Parsable to a Token and maintain all the internal data? In Java it's easy, i can just say:
Token t = parsableList.at(i);
Or in the worst case scenario I can typecast it there:
Token t = (Token)(parsableList.at(i));
No luck in C++ though. Both give me errors.
I tried giving a getId() function to Parsable, but when I do that and call it. the Token's getId function won't override it.
Any suggestions?