[英]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_1
和Derived_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_1
或Derived_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.