Hello just wondering is it possible in C++03 to do something as following. I have tried it but doesn’t work for me.
struct SomeClass{
int a,b,c,d;
};
SomeClass * temp = new SomeClass();
*temp = { 1,2,3,4};
I did it but compiler gives me a warning that extended initalizers are only supported with C++11
Is there some other way to use double brace initalization when using new to create the object?
Constructors are not allowed
I don’t think you can do this directly in C++03, but you can (perhaps) simplify the job compared to just brute force, with something you might think of as an “out of class constructor”:
It’s a pretty easy bet that any reasonably recent (and probably even most pretty old) C++ compiler will inline the function call, so the function call won’t impose any overhead. If you wanted to, you could also turn the function into a template, and overload it for different numbers of parameters, so you could do things like:
Ultimately, however, the big problem here is that you’re starting off on the wrong foot. Chances are that for (at least) the next year or two, you should simply forget that C++ has
newexpressions at all. Feeling like you need it (especially in a case like this) is a fairly good sign that you haven’t adapted to C++ very well yet, and are still trying to write Java.