我有分配与基类指针派生类对象中的问题,使用针对派生类对象的基类指针...上堆与堆
class Base
{
void print() { cout<<"Class Base"; }
};
class Derived: public Base
{
void print() { cout<<"class Derived"; }
};
int main()
{
Base b, *bp;
Derived d, *dp;
b.print();
d.print();
bp = d; // why is this a conversion error? getting an error "cannot convert ‘Derived’ to ‘Base*’ in assignment"
bp = new B(); // this works fine...
}
这是否意味着我们只能分配一个动态将派生类对象分配给基类指针?为什么???
“ap”在哪里申报?这个代码甚至不会编译,因为'ap'没有声明。向我们展示* actual *代码。另外,你不能指定对象指针,你可以指定地址指针。你应该选择一个[好书](http ://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 –
我的坏...它是假设为bp:( –
+1为构造良好的第一篇文章 –