英文:
Can we initialize Base class member struct variable in Derived class
问题
以下是您要翻译的代码部分:
struct Detail{
int cost;
int age;
bool danger;
unsigned char country[3];
}
class Toy{
public:
Toy();
virtual ~Toy();
};
class doll: public Toy
{
public:
doll();
virtual ~doll();
Detail detail={4,12,false,"CN"};
};
I intend to place the Derived object ptr into Base class list
Somewhat like this
std::vector<Toy*> toy_list;
doll* one = new doll();
toy_list.push_back(one);
Leggo* two = new Leggo(); // another class that Derived from Toy
toy_list.push_back(two);
So that I can eventually use the list to obtain value for individual item
for(auto obj: toy_list)
{
printf(" toy is from %s \n", obj->detail.country);
};
希望这有所帮助。
英文:
The question is as above. Let say I have the following code
struct Detail{
int cost;
int age;
bool danger;
unsigned char country=[3];
}
class Toy{
public:
Toy();
virtual ~Toy();
};
I know that I could initialize Detail struct in the Derived class, say doll, in this manner
class doll: public Toy
{
public:
doll();
virtual ~doll();
Detail detail={4,12,false,"CN"};
};
However, I am thinking of how to push Detail struct to the Base class Toy as I have more than one Derived class with differing unique details(the fields of Detail for each Derived are the same but not the value). This sounded more appropriate to me as well.
But how could I do an initialization of the struct from Derived class? I am working on c++ by the way
[EDIT]
I intend to place the Derived object ptr into Base class list
Somewhat like this
std::vector<Toy*> toy list;
doll* one = new doll();
toy_list.push_back(one);
Leggo* two = new Leggo(); // another class that Derived from Toy
toy_list.push_back(two);
So that I can eventually use the list to obtain value for individual item
for(auto obj: toy_list)
{
printf(" toy is from %d \n", obj->detail.country)
};
Thanks
答案1
得分: 3
A derived class can only initialize its own members and direct base classes.
如果detail
是一个基类的成员,那么基类应该提供一个受保护的构造函数来初始化该成员,并且派生类应该使用它。
以下是一些代码示例。我已经将detail
成员设置为const,以证明它仅在派生类构造时初始化:
struct Detail {
int cost;
int age;
bool danger;
unsigned char country[3];
};
class Toy {
protected:
Toy(Detail&& detail) : detail(detail) {};
public:
const Detail detail;
virtual ~Toy() {};
};
class doll : public Toy
{
public:
doll() : Toy(Detail{ 4,12,false,"CN" }) {};
virtual ~doll() {};
};
英文:
A derived class can only initialize its own members and direct base classes.
If detail
is a member of a base class, the base class should provide a
protected constructor able to initialize that member, and the derived
class should use it.
Here is some code showing that. I have made the detail
member const to prove
that is is only initialized at derived class construction:
struct Detail {
int cost;
int age;
bool danger;
unsigned char country[3];
};
class Toy {
protected:
Toy(Detail&& detail) : detail(detail) {};
public:
const Detail detail;
virtual ~Toy() {};
};
class doll : public Toy
{
public:
doll() : Toy(Detail{ 4,12,false,"CN" }) {};
virtual ~doll() {};
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论