Classes from pointers?

Z

zRaze

Scenario:
I have a base class called "BaseCls", and two derived classes called
"DerCls1" and "DerCls2"

I declare a variable -
BaseCls *MyClassObj;

I have some code -

If( Condition )
{
MyClassObj = new DerCls1;
} else {
MyClassObj = new DerCls2;
}

How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
object?

Thanks in advance...
 
J

Jakob Bieling

zRaze said:
Scenario:
I have a base class called "BaseCls", and two derived classes called
"DerCls1" and "DerCls2"

I declare a variable -
BaseCls *MyClassObj;

I have some code -

If( Condition )
{
MyClassObj = new DerCls1;
} else {
MyClassObj = new DerCls2;
}

How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
object?


One way would be to have a virtual function in your base class, that
returns some info that it is the base class. The derived classes are
required to overload it and return info depending on what class they are.

Another way is to use dynamic_cast. If you cast the base class pointer
to the wrong derived class pointer type, you will get a null pointer.

Personally, I would prefer the former method, even tho you have the
disadvantage of requiring all derived classes to overload that function. If
the base class is (or can be) abstract anyway, you could make that make the
'info' function pure virtual even.

hth
 
Z

zRaze

Thanks!

One way would be to have a virtual function in your base class, that
returns some info that it is the base class. The derived classes are
required to overload it and return info depending on what class they are.

Another way is to use dynamic_cast. If you cast the base class pointer
to the wrong derived class pointer type, you will get a null pointer.

Personally, I would prefer the former method, even tho you have the
disadvantage of requiring all derived classes to overload that function. If
the base class is (or can be) abstract anyway, you could make that make the
'info' function pure virtual even.

hth
 
C

Cy Edmunds

zRaze said:
Scenario:
I have a base class called "BaseCls", and two derived classes called
"DerCls1" and "DerCls2"

I declare a variable -
BaseCls *MyClassObj;

I have some code -

If( Condition )
{
MyClassObj = new DerCls1;
} else {
MyClassObj = new DerCls2;
}

How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
object?

Thanks in advance...

If you have to make that distinction you probably have a poor design. When
you have a pointer to a base class which really points to a derived class,
you are using polymorphism. As Jakob implied, the best way to deal with
polymorphism is to call a virtual function which does the right thing no
matter what type it is. This scheme is efficient and maintainable. (For
instance it would be easy to add a DerCls3 later.) If it is impossible
because you can't implement any such virtual function then maybe
polymorphism was the wrong approach in the first place.

A couple of other points: you should probably put a do-nothing virtual
destructor in the base class:

virtual ~BaseCls() {}

to make sure possible destructors in derived classes are called properly.
Also, whenever you use new() you must use delete() to avoid a memory leak
and you must use it only once to avoid undefined behaviour. This can be a
source of problems. Consider using a reference counted smart pointer instead
of a raw pointer for MyClassObj:

typedef boost::shared_ptr<BaseCls> BaseClsPtr;

MyClassObj = BaseClsPtr(new DerCls1);

It will automatically delete the object when the pointer goes out of scope
so you don't have to worry about it.You can get such a pointer at

www.boost.org

Good luck.
 
Z

zRaze

Basically, the reason I need to know what class a pointer points to is
because I have a 2 dimensional array of pointers (it's a map)
and my classes are the landmarks etc. (Road, open space etc.)

So therefore, I don't have the problem with adding new classes, but
when I come do save the map to disk, I need to be able to tell what is
on that tile for when reloading.

I have now got a new method of this. I had a function that returned a
graphic handle (for drawing on the screen), however, I was having
problems (which I thought was related, but wasn't) so I changed it to
return an enum value which gets linked to the graphic, and I'm just
going to use that enum value...

Thanks for your help tho...
 
J

jeffc

zRaze said:
Scenario:
I have a base class called "BaseCls", and two derived classes called
"DerCls1" and "DerCls2"

I declare a variable -
BaseCls *MyClassObj;

I have some code -

If( Condition )
{
MyClassObj = new DerCls1;
} else {
MyClassObj = new DerCls2;
}

How can I tell if MyClassObj is pointing to a DerCls1 or DerCls2
object?

This is not a hard and fast rule, but one of the basic principles OO code
such as that is you're not *supposed* to know. And you're supposed to write
your code in such a way that the code doesn't care either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top