指向派生类对象的基类指针的类型

Type of Base class pointer pointing to derived class object

本文关键字:基类 类型 指针 对象 派生      更新时间:2023-10-16

指向派生类对象的基类指针的类型是否更改?

如果我有类似的东西:

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

然后我做以下任务:

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

指针b的类型会变为派生的指针吗?还是保持不变,即指向基的指针?为什么?

代码应该是这样的:

          class A
          {};
         class B:public A
         {};

         int main()
         {
           A* a= new B();
           cout<<typeid(a).name()<<endl;
        }

输出:A类*。

因为类型意味着指针的类型(它是基*(,而不是它所指向的对象的类型。