Hello
I have a class with copy assignment operator(s):
Is there any way to write this code in one line?Code:someclass instance1("construction"); someclass instance2 = instance1;
Hello
I have a class with copy assignment operator(s):
Is there any way to write this code in one line?Code:someclass instance1("construction"); someclass instance2 = instance1;
well if you want to copy something, it takes at least one statement. also to copy something, you have to have it first, which also takes the one statement prior.
you could also have a constructor that takes in a 'someclass', but that basically does the same thing as what you have here. i cant think of a way to reduce it to one statement.
heres an example, note you still need to have the statement prior that declares instance1Code:someclass instance2 = someclass(instance1);
You only use two constructors in your example code: one to construct a someclass object given a string, the other is the copy constructor. The copy assignment operator is not used.I have a class with copy assignment operator(s):
Well, unless there are some unusual semantics you could write:Is there any way to write this code in one line?
I would rather have it on two lines though.Code:someclass instance1("construction"), instance2("construction");
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
When you are initializing (rather than assigning already assigned variables), the basic constructor can be used, so "X x = X()" doesn't need the assignment operator, just a basic constructor call.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
In this context, no.isnt the '=' the copy operator in action?
is equivalent to:Code:someclass instance2 = instance1;
except that the former is not allowed if the copy constructor is declared explicit.Code:someclass instance2(instance1);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)