simple inheritance(?) question

M

Michael

Hi All,

if I have a class BaseClass and two derived classes Class1 and Class2 and I
don't know which derived class I will need, is it ok for me to do the
following:

BaseClass* base;

if(some_test){
base = new Class1;
} else {
base = new Class2;
}

I know it works, but is it somehow dodgy? I have a feeling there is a better
way but I'm not sure what to search for.

Thanks for your help.

Regards

Michael
 
D

dasteph

Hi All,

if I have a class BaseClass and two derived classes Class1 and Class2 and I
don't know which derived class I will need, is it ok for me to do the
following:

BaseClass* base;

if(some_test){
base = new Class1;} else {

base = new Class2;

}

I know it works, but is it somehow dodgy? I have a feeling there is a better
way but I'm not sure what to search for.

Thanks for your help.

Regards

Michael


What you have discovered is the base class pointer. What you are doing
is a common practiced method for polymorphism. Just have a look at
http://www.cplusplus.com/doc/tutorial/polymorphism.html for instance.
Keep in Mind that with a base class pointer you can only access
members that are defined in the base class.

Regards

Stephan
 
P

Puppet_Sock

Hi All,

if I have a class BaseClass and two derived classes Class1 and Class2 and I
don't know which derived class I will need, is it ok for me to do the
following:

BaseClass* base;

if(some_test){
base = new Class1;} else {

base = new Class2;

}

I know it works, but is it somehow dodgy? I have a feeling there is a better
way but I'm not sure what to search for.

As another poster has said already, this is standard
stuff in C++. You just have to be sure that the classes
involved have the right stuff as regards virtual functions,
public/protected/private members, etc.

You want to read the FAQ here.
http://www.parashift.com/c++-faq-lite/
Scroll down to the parts about inheritance.
Socks
 
S

SasQ

Dnia Fri, 09 Mar 2007 04:19:28 -0800, dasteph napisa³(a):
What you have discovered is the base class pointer. What you are
doing is a common practiced method for polymorphism. Just have a
look at http://www.cplusplus.com/doc/tutorial/polymorphism.html
for instance. Keep in Mind that with a base class pointer you can
only access members that are defined in the base class.

But keep in mind also that if that members are virtual
member functions, the overriden functions from the derived
classes will be called ;)
 
M

Michael

What you have discovered is the base class pointer. What you are doing
is a common practiced method for polymorphism. Just have a look at
http://www.cplusplus.com/doc/tutorial/polymorphism.html for instance.
Keep in Mind that with a base class pointer you can only access
members that are defined in the base class.

Regards

Stephan

Thanks for your help.

I am a bit confused by my ability to use a member attribute that is only
present in Class1, not in BaseClass, after I do the base = new Class1.

I would have expected that I could only access functions and variables
defined in the BaseClass, and that any additional functionality (including
additional attributes) added by Class1 or Class2 would be lost, but this
appears to not be the case. It seems that since I am using the redefined
function from Class1 it also has access to the member attributes from
Class1. Would this be correct?

Can anyone explain why for me?

Thanks

Michael
 
A

Adrian Hawryluk

Michael said:
I am a bit confused by my ability to use a member attribute that is only
present in Class1, not in BaseClass, after I do the base = new Class1.

I would have expected that I could only access functions and variables
defined in the BaseClass, and that any additional functionality (including
additional attributes) added by Class1 or Class2 would be lost, but this
appears to not be the case. It seems that since I am using the redefined
function from Class1 it also has access to the member attributes from
Class1. Would this be correct?

Yes it is, but only if the that redefined function is virtual.
Can anyone explain why for me?

Because it is virtual, when that function is called, it goes through the
vtable (virtual table of function pointers) to call the function. It
there by 'knows' what object it really is.


Adrian
 
M

Michael

Adrian Hawryluk said:
Yes it is, but only if the that redefined function is virtual.


Because it is virtual, when that function is called, it goes through the
vtable (virtual table of function pointers) to call the function. It
there by 'knows' what object it really is.


Adrian

ahhhhh......ok....thanks
 

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
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top