Glo*_*ove 7 c++ variables abstract-class types declaration
假设foo是C++程序中的摘要 class,为什么声明类型的变量foo*而不是类型的变量是可接受的foo?
您不能实例化一个抽象类。以下声明之间存在差异。
// declares only a pointer, but do not instantiate.
// So this is valid
AbstractClass *foo;
// This actually instantiate the object, so not valid
AbstractClass foo;
// This is also not valid as you are trying to new
AbstractClass *foo = new AbstractClass();
// This is valid as derived concrete class is instantiated
AbstractClass *foo = new DerivedConcreteClass();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12470 次 |
| 最近记录: |