![](/img/trans.png)
[英]How to use a member in the dervied class which assign in the constuctor of the base class?
[英]is it possible to assign base class's struct to dervied class's struct
我有一個類A,該類具有成員結構,另一個類B繼承了類A,而類B的結構繼承了類A的結構。
class A
{
public:
struct st
{
int x;
int y;
};
};
class B : public A
{
public:
struct st : A::st
{
int z;
};
};
以下代碼給我錯誤:做這件事的方式是什么
B::st* obj = NULL;
obj = new A::st [10];
您正在嘗試的是錯誤的,因為B::st
是A::st
子類型。 因此,指向A::st
的指針不能自動轉換為B::st
類型的指針。
出於同樣的原因,您不能使用:
B* bPtr = new A;
您可以采取其他方法。
A::st* obj = NULL;
obj = new B::st; // Don't use the array new. That is going to be problematic.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.