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. I am going to allocate and instantiate the struct using the new
keyword. When I do so, I want to construct each myObject by name in an initializer list, as shown below.
// 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.
Thanks!
EDIT - CONCLUSION
As pointed out, my initialization list is C-style, which fails to work. 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.
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!