在派生类中可以初始化基类成员结构变量吗?

huangapple go评论65阅读模式
英文:

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,&quot;CN&quot;};
};

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&lt;Toy*&gt; 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(&quot; toy is from %d \n&quot;, obj-&gt;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&amp;&amp; detail) : detail(detail) {};

public:
    const Detail detail;

    virtual ~Toy() {};
};

class doll : public Toy
{
public:
    doll() : Toy(Detail{ 4,12,false,&quot;CN&quot; }) {};
    virtual ~doll() {};

};

huangapple
  • 本文由 发表于 2023年5月11日 13:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定