[英]C++ Initialize object member of a struct that is allocated using the “new” keyword
This may be a complicated question but I assume it has a simple answer. 这可能是一个复杂的问题,但我认为它有一个简单的答案。
So I have a struct that contains custom object types: 所以我有一个包含自定义对象类型的结构:
// header1.h
struct MyStruct
{
MyClass myObject1;
MyClass myObject2;
};
And I have a global pointer to a MyStruct. 而且我有一个指向MyStruct的全局指针。 I am going to allocate and instantiate the struct using the new
keyword. 我将使用new
关键字分配和实例化结构。 When I do so, I want to construct each myObject by name in an initializer list, as shown below. 这样做时,我想按名称在初始化列表中构造每个myObject,如下所示。
// codeFile1.cpp
MyStruct *myStructPtr;
int main()
{
myStructPtr = new MyStruct
{ //syntax error: missing ';'
.myObject1 = MyClass(arg1, arg2),
.myObject2 = MyClass(arg1, arg2)
};
}
The code snippet above has syntax errors, but it demonstrates what I want to do. 上面的代码段有语法错误,但是它演示了我想要做什么。 Note that MyClass does not have a default constructor, so it must be given an argument list. 请注意,MyClass没有默认的构造函数,因此必须给它一个参数列表。
Thanks! 谢谢!
EDIT - CONCLUSION 编辑-结论
As pointed out, my initialization list is C-style, which fails to work. 如前所述,我的初始化列表是C样式的,无法正常工作。 Unfortunately the C++11 initializer-list does not meet my compiler requirements, and the option of throwing all arguments into a constructor is not practical. 不幸的是,C ++ 11的初始值设定项列表不满足我的编译器要求,并且将所有参数都放入构造函数中的选择不切实际。
So I took an alternative solution and changed the pointer structure to the following: 因此,我采取了一种替代解决方案,并将指针结构更改为以下内容:
// header1.h
struct MyStruct
{
MyClass *myObjectPtr1;
MyClass *myObjectPtr2;
};
// codeFile1.cpp
MyStruct myStruct;
int main()
{
myStructPtr.myObjectPtr1 = new MyClass(arg1, arg2);
myStructPtr.myObjectPtr2 = new MyClass(arg1, arg2);
}
Thanks all! 谢谢大家!
If MyStruct
is an aggregate, you could use aggregate initialization to avoid defining a constructor: 如果MyStruct
是聚合,则可以使用聚合初始化来避免定义构造函数:
myStructPtr = new MyStruct {{arg1, arg2}, {arg1, arg2}};
If not, please provide a constructor taking the appropriate arguments and call 如果没有,请提供一个带有适当参数的构造函数并调用
myStructPtr = new MyStruct{arg1, arg2};
You're trying to use C's designated initializers, which are not part of C++ . 您正在尝试使用C的指定初始化器,它们不是C ++的一部分 。 If you have a C++11 compiler, you can simply do the following: 如果您具有C ++ 11编译器,则可以执行以下操作:
MyStruct *myStructPtr = new MyStruct
{
MyClass(arg1, arg2),
MyClass(arg1, arg2)
};
You will, somehow, need to chain your constructions together. 您将需要以某种方式将您的结构链接在一起。 Assuming you actually want the same arguments: 假设您实际上想要相同的参数:
struct MyStruct
{
MyStruct(int arg1, int arg2) : myObject(arg1, arg2), myObejct2(arg1, arg2) {}
MyClass myObject1;
MyClass myObject2;
};
MyStruct *s = new MyStruct(34, 42);
Or something like this: 或类似这样的东西:
struct MyStruct
{
MyStruct(const MyClass& a1, const MyClass& a2) : myObject(a1), myObejct2(a2) {}
....
};
MyStruct *s = new MyStruct(MyClass(1,2), MyClass(3,4));
or in C++11 initializer lists: 或在C ++ 11初始化程序列表中:
MyStruct *s = new MyStruct({1,2}, {3,4});
Many other similar solutions are available. 还有许多其他类似的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.