G Gill
G Gill

Reputation: 1107

Type of Base class pointer pointing to derived class object

Does the type of base class pointer pointing to the derived class object change?

If I have something like:

class base{
public:
    int a;
    void doit();
};

class derived : public base {
public:
    int a,b;
    void doit();
}

And then I do the following assignment:

base *b = new derived;
cout << typeof(b); 

Will the type of pointer b change to pointer of derived or does it stay the same i.e pointer to base? and why?

Upvotes: 0

Views: 394

Answers (1)

Rakib
Rakib

Reputation: 7625

The code should be like this:

          class A
          {};

         class B:public A
         {};


         int main()
         {

           A* a= new B();

           cout<<typeid(a).name()<<endl;
        }

output: class A* .

Because type implies the type of the pointer ( which is base*), not the type of the object to which it points.

Upvotes: 2

Related Questions