Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top