Base Class As A Generic Manipulation Interface

Discussion in 'C++' started by Marcelo De Brito, May 20, 2009.

  1. Hi!

    Can I use a base class as a generic interface for manipulation of
    derived classes from it?

    I have noticed that even classes that do not inherit "directly" from
    the base class can be manipulated through a base class pointer, of
    course taking into account all the virtual stuffs are done properly.
    For example:

    class B1 {
    public:
    B1() {}
    virtual ~B1() {}
    virtual void f() {}
    };

    class D1 : public B1{
    public:
    D1() {}
    virtual ~D1() {}
    void f() {}
    };

    class D2 : public D1{
    public:
    D2() {}
    virtual ~D2() {}
    void f() {}
    };

    int main()
    {
    D2 dd2;
    B1* bb1 = &dd2;
    //OR B1* bb1 = new D2;
    bb1->f(); //calls D2's "f()" function
    }

    From what I have tested so far, all the public methods inside D2 can
    be manipulated through the "B1" pointer "bb1", even though both
    classes does not have a "direct" relationship (I know they have
    through D2).

    What kinds of problems can arise from that approach? What about the
    safety of using it? Is it safe?

    I apprecite any comment, suggestion, etc.

    Thank You!

    Marcelo
    Marcelo De Brito, May 20, 2009
    #1
    1. Advertising

  2. Marcelo De Brito

    Neelesh Guest

    On May 20, 8:20 pm, Marcelo De Brito <> wrote:
    > Hi!
    >
    > Can I use a base class as a generic interface for manipulation of
    > derived classes from it?
    >
    > I have noticed that even classes that do not inherit "directly" from
    > the base class can be manipulated through a base class pointer, of
    > course taking into account all the virtual stuffs are done properly.
    > For example:
    >
    > class B1 {
    >   public:
    >     B1() {}
    >     virtual ~B1() {}
    >     virtual void f() {}
    >
    > };
    >
    > class D1 : public B1{
    >   public:
    >     D1() {}
    >     virtual ~D1() {}
    >     void f() {}
    >
    > };
    >
    > class D2 : public D1{
    >   public:
    >     D2() {}
    >     virtual ~D2() {}
    >     void f() {}
    >
    > };
    >
    > int main()
    > {
    >   D2 dd2;
    >   B1* bb1 = &dd2;
    >   //OR B1* bb1 = new D2;
    >   bb1->f(); //calls D2's "f()" function
    >
    > }
    >
    > From what I have tested so far, all the public methods inside D2 can
    > be manipulated through the "B1" pointer "bb1", even though both
    > classes does not have a "direct" relationship (I know they have
    > through D2).
    >


    First, what do you mean by "manipulating a method"? If you mean
    "calling a method" then only virtual functions are dynamically bound,
    which means that if a function foo() is virtual in base class then and
    only then the derived class version can be called using base class
    reference or pointer (Of course provided that the actual object
    associated with that pointer or reference is the derived class
    object). This holds only for public inheritance.

    Also, what do you mean by "direct" and "indirect" relationship? Since
    D1 is publicly inherited from B1, hence they share "D1 is a B1"
    relationship. Similarly since D2 is publicly derived from D1, hence
    they share "D2 is a D1" relationship. Combining the two, we observe
    that "D2 is a B1". Hence there is no problem in using B1* to point to
    a D2 object and accessing D2's virtual function using this pointer.

    > What kinds of problems can arise from that approach? What about the
    > safety of using it? Is it safe?


    I am not sure about what "safety" parameters you are talking about,
    but this is perfectly legal and in fact one of the important features
    of C++.
    Neelesh, May 20, 2009
    #2
    1. Advertising

  3. Hi, Neelesh!

    By "manipulation/manipulating", I mean "calling". Sorry for using such
    a general word, but I did it for the sake of generality. :)

    By "direct/indirect" relationships, I mean, respectively, a class that
    explicitly inherits from another (the case of D1 which inherits from
    B1) and a class that "implicitly" inherits from another (the case of
    D2 which does not "inherit" B1 as D1 does, but does it through D1 -- I
    think). Sorry again here for my ill explanation, but I hope you have
    understood.

    By safety, I mean what kinds of bugs could happen when using such
    approach.

    By the way, extremely elegant that generic interface feature C++ has
    to offer us. :)

    Thank You!

    Marcelo
    Marcelo De Brito, May 20, 2009
    #3
    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. Victor Hannak
    Replies:
    1
    Views:
    329
    Victor Bazarov
    Aug 25, 2003
  2. Replies:
    2
    Views:
    435
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    544
    John Carson
    Sep 3, 2005
  4. Hicham Mouline
    Replies:
    1
    Views:
    592
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    139
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page