473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Classes from pointers?

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...
Jul 19 '05 #1
5 2054
"zRaze" <ab***@msn.com> wrote in message
news:o2********************************@4ax.com...
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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2
Thanks!

On Sat, 11 Oct 2003 18:05:13 +0200, "Jakob Bieling" <ne*****@gmy.net>
wrote:
"zRaze" <ab***@msn.com> wrote in message
news:o2********************************@4ax.com.. .
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


Jul 19 '05 #3
"zRaze" <ab***@msn.com> wrote in message
news:o2********************************@4ax.com...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #4
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...

On Sun, 12 Oct 2003 02:47:54 GMT, "Cy Edmunds"
<ce******@spamless.rochester.rr.com> wrote:
"zRaze" <ab***@msn.com> wrote in message
news:o2********************************@4ax.com.. .
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.


Jul 19 '05 #5

"zRaze" <ab***@msn.com> wrote in message
news:o2********************************@4ax.com...
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.
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
I have this class ------------- class Component { /*class*/ Data *d; /*class*/ Draw *a; }; ------------- from "Component" derive classes like "TextBox", "Button", "Label", "ComboBox" etc from...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
3
by: John J | last post by:
I requested help with some code in a previous thread, as requested in the feedback, below are the .cpp and .h files for all three classes (Entry, Race and Yacht). The three classes are all...
7
by: Sean J. Fraley | last post by:
This code illustrates what I'm confused about: template<typename T> class foo { public: template<typename U> void fooFunction(const foo<U>& x) {
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
7
by: alternativa | last post by:
Hello, I have a few questions concerning classes. 1) Why some people use default constructos, i.e constructors with no parameters? To me it doesn't make any sense, is there something I should...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.