simple inheritance(?) question

Discussion in 'C++' started by Michael, Mar 9, 2007.

  1. Michael

    Michael Guest

    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
    Michael, Mar 9, 2007
    #1
    1. Advertising

  2. Michael

    Guest

    On 9 Mrz., 11:22, "Michael" <> wrote:
    > 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
    , Mar 9, 2007
    #2
    1. Advertising

  3. Michael

    Puppet_Sock Guest

    On Mar 9, 5:22 am, "Michael" <> wrote:
    > 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
    Puppet_Sock, Mar 9, 2007
    #3
  4. Michael

    SasQ Guest

    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 ;)

    --
    SasQ
    SasQ, Mar 9, 2007
    #4
  5. Michael

    Michael Guest

    <> wrote in message
    news:...
    > On 9 Mrz., 11:22, "Michael" <> wrote:
    >> 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
    >


    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
    Michael, Mar 10, 2007
    #5
  6. Michael wrote:
    >
    > 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
    Adrian Hawryluk, Mar 10, 2007
    #6
  7. Michael

    Michael Guest

    "Adrian Hawryluk" <> wrote in message
    news:8PnIh.52128$cE3.17324@edtnps89...
    > Michael wrote:
    >>
    >> 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


    ahhhhh......ok....thanks
    Michael, Mar 10, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,114
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    801
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,023
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,872
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    759
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page