-2

Assume there are class A , and class B that inherits A. what is the correct way to use B objects? I've seen in "cplusplus.com" that they are "using" B objects this way:

B b;
A* a=&b;

What's the difference between using a class "A" pointer and using b?

Thanks!

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Shon Top
  • 11
  • 3

2 Answers2

2

They only did that to show that you can, and that polymorphism is invoked when you do.

If you just want to use your B b, just use it; no A* required.

As an aside, learning C++ from internet tutorials is rather akin to learning how to cook by analysing some chewing gum you found in the street. Prefer a proper, peer-reviewed book.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

The advantage of using pointers to base classes comes when you have virtual functions. Imagine the following

class A {
    A(){}
    virtual ~A(){} // note virtual destructor
    virtual void func(){ cout << "A func" << endl; }
};

class B : public A {
    B(){}
    virtual ~B(){}
    virtual void func(){ cout << "B func" << endl; }
};

class C : public A {
    C(){}
    virtual ~C(){}
    virtual void func(){ cout << "C func" << endl; }
}

now if we have a pointer to base class we can call func() on it and the correct function is called depending on whether the pointer actually points at an A, a B or a C. Example:

A* p1 = new A;
A* p2 = new B;
A* p3 = new C;
p1->func();
p2->func();
p3->func();

will output the following

A func
B func
C func

Now you may say why not use three different pointers to A, B and C types separately but what if you wanted an array of pointers? Then they would all need to be the same type, namely A*. This type of polymorphism is useful for grouping things which are similar in function or spirit but different in implementation.

Dan
  • 12,857
  • 7
  • 40
  • 57