指向派生类对象的基类指针的类型
Type of Base class pointer pointing to derived class object
指向派生类对象的基类指针的类型是否更改?
如果我有类似的东西:
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类*。
因为类型意味着指针的类型(它是基*(,而不是它所指向的对象的类型。
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- 当基类是依赖类型时,这是一个缺陷吗
- 如何使用 std::make_shared 创建基类类型的智能指针?
- 为什么允许dynamic_cast到非唯一的基类类型?
- 基类类型向量中的派生结构
- C++ 模板:重载时找不到基类类型参数方法
- 如果类包含基类类型的成员作为第一个元素,后跟其他成员,编译器是否可以优化空基?
- 如何在不转换回基类类型的情况下返回派生类型的对象?
- 声明基类类型的指针,但随后通过指向子类来实例化它.这是良好的编程实践吗?
- 获取继承基类类型的元组
- 如何从指针调用派生类函数到基类类型 (C++)
- 如何检查对象是否是基类类型
- 来自存储在带有基类类型的地图中的派生类的调用函数
- 采用基类类型参数的虚拟函数
- 用派生类填充基类类型的2D矢量
- 基类类型的指针忘记了原始对象类型
- 在c++中推回基类类型2d vector中的子类对象
- 子类对象,用作定义为c++基类类型的参数
- 从基类类型的向量访问派生类方法
- 从基类类型的指针数组调用派生类的方法.(c++)