是否可以在 class 构造函数中调用 Class 成员构造函数?
Is it possible to call Class member constructor inside class constructor?
我希望能够根据通过 class 构造函数收到的条件,以 N 种方式之一(在此示例中为 N=2)初始化 class 成员,如下面的代码,但是 MainObject 的初始化似乎只是 (container) class' 构造函数的局部。我想知道这个特定模式的最佳实践是什么。
// in ContainerObject.hpp
class ContainerObject {
public:
MainClass MainObject;
ContainerObject(int type);
}
// in ContainerObject.cpp
ContainerObject::ContainerObject(int type);{
if (type == 0){
MainObject("something", 1, 2);
} else if (type == 1){
MainObject(4, "another thing", "yet another thing");
}
}
到目前为止我已经考虑过
- 将主要对象放入堆中
- 定义 N class 构造函数并在“主”/“第一个”class 构造函数中递归调用适当的构造函数。
请注意,“0”和“1”初始化只是一个示例,可能会有很大不同。
EDIT1:添加了“;”编译需要
编辑2:
改了原文
//...
if (type == 0){
MainObject(0);
} else if (type == 1){
MainObject(1);
}
//...
当前的
//...
if (type == 0){
MainObject("something", 1, 2);
} else if (type == 1){
MainObject(4, "another thing", "yet another thing");
}
//...
因为它被误解为可以通过添加以下内容解决的案例而被称为重复。
//...
ContainerObject(int type): MainObject(type);
//...
我将问题解释为“如何执行重要逻辑 before/during 成员初始化列表”。
解决这个问题的一个好方法是将外部对象的构造函数参数转换为子对象的工作委托给实用函数:
// in ContainerObject.cpp
#include <stdexcept> // for std::invalid_argument
// Anonymous namespace since main_factory() is only needed in this TU.
namespace {
MainClass main_factory(int type) {
if (type == 0) {
return MainClass("something", 1, 2);
} else if (type == 1) {
return MainClass(4, "another thing", "yet another thing");
}
// N.B. This is one of the scenarios where exceptions are indisputably
// the best way to do error handling.
throw std::invalid_argument("invalid type for MainClass");
}
}
ContainerObject::ContainerObject(int type)
: MainObject(main_factory(type)) {}
每当创建对象时,总是会自动调用构造函数。您可以从不在任何地方自己调用它。
我希望能够根据通过 class 构造函数收到的条件,以 N 种方式之一(在此示例中为 N=2)初始化 class 成员,如下面的代码,但是 MainObject 的初始化似乎只是 (container) class' 构造函数的局部。我想知道这个特定模式的最佳实践是什么。
// in ContainerObject.hpp
class ContainerObject {
public:
MainClass MainObject;
ContainerObject(int type);
}
// in ContainerObject.cpp
ContainerObject::ContainerObject(int type);{
if (type == 0){
MainObject("something", 1, 2);
} else if (type == 1){
MainObject(4, "another thing", "yet another thing");
}
}
到目前为止我已经考虑过
- 将主要对象放入堆中
- 定义 N class 构造函数并在“主”/“第一个”class 构造函数中递归调用适当的构造函数。
请注意,“0”和“1”初始化只是一个示例,可能会有很大不同。
EDIT1:添加了“;”编译需要 编辑2: 改了原文
//...
if (type == 0){
MainObject(0);
} else if (type == 1){
MainObject(1);
}
//...
当前的
//...
if (type == 0){
MainObject("something", 1, 2);
} else if (type == 1){
MainObject(4, "another thing", "yet another thing");
}
//...
因为它被误解为可以通过添加以下内容解决的案例而被称为重复。
//...
ContainerObject(int type): MainObject(type);
//...
我将问题解释为“如何执行重要逻辑 before/during 成员初始化列表”。
解决这个问题的一个好方法是将外部对象的构造函数参数转换为子对象的工作委托给实用函数:
// in ContainerObject.cpp
#include <stdexcept> // for std::invalid_argument
// Anonymous namespace since main_factory() is only needed in this TU.
namespace {
MainClass main_factory(int type) {
if (type == 0) {
return MainClass("something", 1, 2);
} else if (type == 1) {
return MainClass(4, "another thing", "yet another thing");
}
// N.B. This is one of the scenarios where exceptions are indisputably
// the best way to do error handling.
throw std::invalid_argument("invalid type for MainClass");
}
}
ContainerObject::ContainerObject(int type)
: MainObject(main_factory(type)) {}
每当创建对象时,总是会自动调用构造函数。您可以从不在任何地方自己调用它。