Base Class As A Generic Manipulation Interface

M

Marcelo De Brito

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
 
N

Neelesh

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++.
 
M

Marcelo De Brito

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top