简体   繁体   English

如何在派生类中初始化需要构造函数参数的成员对象?

[英]How to initialize member objects that requires constructor parameters in derived classes?

I would like to include a Composed object in my Base class, and have different derived classes have different configurations of it. 我想在我的Base类中包含一个Composed对象,并让不同的派生类具有不同的配置。 However the Composed class requires parameter in constructor, and I don't have control over its architecture. 但是, Composed类需要在构造函数中使用参数,并且我无法控制其体系结构。

The compiler is reporting an error because the parameter for Composed should be passed at the initialization of Base class. 编译器报告错误,因为应该在初始化Base类时传递用于Composed的参数。 But I only want it to be initialized in derived classes. 但我只希望在派生类中对其进行初始化。 What should I do in this case? 在这种情况下我该怎么办?

class Base{
  public:
    Base(); //Error
  protected:
    Composed comp;
};
class Composed{
  public:
    Composed(int id):id(id);
  private:
    int id;
};
class Derived_1:public Base{
  public:
    Derived():comp(1234){};
};    
class Derived_2:public Base{
  public:
    Derived():comp(2345){};
};   

You'll have to pass that parameter up to Base, and then hand it down to comp: 您必须将该参数传递给Base,然后将其传递给comp:

class Composed{
  public:
    Composed(int id):id(id);
  private:
    int id;
};
class Base{
  public:
    Base(int id):comp(id){}
  protected:
    Composed comp;
};
class Derived_1:public Base{
  public:
    Derived():Base(1234){};
};    
class Derived_2:public Base{
  public:
    Derived():Base(2345){};
}; 

Imagine you constructed a Base object. 假设您构造了一个Base对象。 How would it know what parameter to pass to comp 's constructor? 如何知道要传递给comp的构造函数的参数? The Base class is fundamentally flawed, that's why you have an error. Base类从根本上来说是有缺陷的,这就是为什么您有一个错误。 It needs to know how to construct it's Composed member object. 它需要知道如何构造它的Composed成员对象。

You can add a default constructor for Composed , and then modify this object in Derived_1 and Derived_2 . 您可以为Composed添加默认构造函数,然后在Derived_1Derived_2修改此对象。 Alternatively, as others suggested, you can make Base 's constructor take the parameters to construct comp . 另外,正如其他人所建议的那样,您可以让Base的构造函数使用参数来构造comp This sounds preferable to me, but it depends on your use case. 这听起来对我来说更好,但这取决于您的用例。

Remember that when you are constructing Derived_1 or Derived_2 , the Base class is the first thing that is created, before other member variables. 请记住,在构造Derived_1Derived_2 ,首先创建Base类,然后是其他成员变量。

There are a few options. 有一些选择。 The most straightforward is to pass the objects required to build Comp to the Base class constructor. 最直接的方法是将构建Comp所需的对象传递给Base类构造函数。

class Base{
  public:
    Base(int id):comp(id){}
protected:
    Composed comp;
};

class Derived_1:public Base{
  public:
    Derived():Base(1234){}
};    
class Derived_2:public Base{
  public:
    Derived():Base(2345){}
};   

Another possibility is not to include Comp in the base class. 另一种可能性是不在基类中包含Comp。 Put it in each derived class. 将其放在每个派生类中。 You can add a pure virtual function to the Base class that returns Comp (or its address). 您可以将一个纯虚函数添加到返回Comp(或其地址)的Base类。

One more option is to store a pointer to Comp (perhaps a unique_ptr) and allocate it in the derived classes. 另一种选择是存储指向Comp的指针(也许是unique_ptr),并将其分配给派生类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用构造函数的参数初始化向量成员 - Initialize vector member with the parameters of constructor 将 class 成员初始化为层次结构中的不同派生类 - Initialize class member to different derived classes in a hierarchy 如何在构造函数中转发所有未知参数以初始化成员对象? - How to forward all unknown parameters in constructor to initialize the member object? 当我希望调用基类构造函数时,如何在派生类中初始化成员const引用? - How can I initialize a member const reference in a derived class, when I wish to call a base class constructor? 如何在派生类构造函数中初始化基类成员变量? - How can I initialize base class member variables in derived class constructor? 如何将具有不同成员变量类型和不同构造函数的两个类制作成派生/基类或模板类? - How to make two classes with different member variable type and different constructor into derived/base class or template class? 如何在派生类中初始化静态成员? - How to initialize static members in derived classes? 如何使用 for 循环从包含不同派生类的基本 class 数组初始化对象? - How can I initialize objects from base class array, containing different derived classes, using a for loop? 如何在构造函数中初始化对象数组? - How to initialize array of objects in constructor? 如何在派生类上强制执行静态成员? - How to enforce a static member on derived classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM