|
This thread is locked; no one can reply to it. |
1
2
|
A few C++ basics |
james_lohr
Member #1,947
February 2002
|
Thanks. Now onto inheritance: Suppose X,Y are subclasses of A, and B has a pointer to a class of type A (which may be instanciated with a subclass X or Y):
In the above, b.sizeOfIt() will return the size of the parent class instead of the actual child class. How would you usually deal with this? How do you know which subclass *a is pointing to? [edit] Should I rather use generic class / templates for this?
|
ImLeftFooted
Member #3,935
October 2003
|
Quote: How do you know which subclass *a is pointing to? The correct is answer is you should not know (or care). That said it is still possible to find out. Heres a more 'proper' soultion:
Given these changes you should be specifying virtual desctructors as well, otherwise calling 'delete' will have undefined results. Edit C(A *_a){ a = _a; } Could be rewritten C(A *_a) : a(a_) { }
|
X-G
Member #856
December 2000
|
That's right, generally the point is that you shouldn't have to know. If you really want to find out the type, GNU C++ does have a typeof operator. -- |
Kibiz0r
Member #6,203
September 2005
|
You should never have to use a parent class to access child-only functionality. This is why virtual functions exist! That said, dynamic_cast will cast further along the inheritance chain, but it's something that should be avoided unless it absolutely cannot be helped, and I can think of very few instances where it can't. --- |
james_lohr
Member #1,947
February 2002
|
Yeah, I realised that I was inventing an exceptional case when in fact overriding was the way to go. Quote: you should be specifying virtual desctructors as well, otherwise calling 'delete' will have undefined results. Great thanks! That one slipped me by: I'll need to fix that right away.
|
Tobias Dammers
Member #2,604
August 2002
|
Rule: If a class has any virtual member functions, the destructor must be virtual, too. Quote: Well this function probably already exists in a header, as default parameters can only be specified in a function definition (this is Java style). If you were to separate the function into a header definition and implement the function elsewhere in some cpp file the function would probably look like this: You mean 'declaration', not 'definition', right? The definition says what it is, the declaration says that is exists and what it looks like. Quote: The correct is answer is you should not know (or care). That said it is still possible to find out. While this is generally true, there are some situations where, for whatever reasons, you need a quick way to find out the data type of a polymorphic class instance. One example may be collision response in a game: depending on what kind of object something collides WITH, the response may be different. A very fast yet easy way is to have a virtual cMyClass::get_type() const function that returns a different type ID (say from an enum) for each class. It's often faster than RTTI. The border isn't really that clear BTW: If, for example, your class can save itself to a chunk file, it probably comes with a get_chunk_id() function anyway, which can easily double as type information. --- |
|
1
2
|