我有分配與基類指針派生類對象中的問題,使用針對派生類對象的基類指針...上堆與堆
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爲構造良好的第一篇文章 –