Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A few C++ basics

This thread is locked; no one can reply to it. rss feed Print
 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):

1 
2class A{
3 public:
4 int a,b,c;
5};
6 
7class X:public A{
8 public:
9 int x,y,z;
10};
11 
12class Y:public A{
13 public:
14 int x,y,z;
15};
16 
17class B{
18 
19 public:
20 C(A *_a){
21 a = _a;
22 }
23 
24 int sizeOfIt(){
25 return sizeof(*a);
26 }
27 
28 A *a;
29};
30 
31/////////////////
32 
33B b(new X());
34 
35return b.sizeOfIt();

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
avatar

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:

1class A{
2 public:
3 int a,b,c;
4 virtual int sizeOfMe() { return sizeof(*this); }
5};
6 
7class X:public A{
8 public:
9 int x,y,z;
10 virtual int sizeOfMe() { return sizeof(*this); }
11};
12 
13class Y:public A{
14 public:
15 int x,y,z;
16 virtual int sizeOfMe() { return sizeof(*this); }
17};
18 
19class B{
20 
21 public:
22 C(A *_a){
23 a = _a;
24 }
25 
26 int sizeOfIt(){
27 return a->sizeOfMe();
28 }
29 
30 A *a;
31};
32 
33/////////////////
34 
35B b(new X());
36 
37return b.sizeOfIt();

Given these changes you should be specifying virtual desctructors as well, otherwise calling 'delete' will have undefined results.

Edit
You have a perfect case here to use : intializer lists.

        C(A *_a){
            a = _a;
        }

Could be rewritten

        C(A *_a) : a(a_) {
            
        }

X-G
Member #856
December 2000
avatar

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.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Kibiz0r
Member #6,203
September 2005
avatar

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
avatar

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.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

 1   2 


Go to: