+ 3
Copy or move constructor
Please refer below code : I am wondering why any of the constructor is not called? Clstest() being temporary object , I was expecting to have call of move constructor while object is created Why no output is printed in this case ? https://code.sololearn.com/c1YUpZwkDDg4/?ref=app
15 Réponses
+ 2
Now every constructor and method is used/printed. Thanks for this nice lesson Ketan Lalcheta
https://code.sololearn.com/co74UfwLEtKC/?ref=app
+ 1
Not sure, but have you tried to make objects from your class. Like this:
int main()
{
// clsTest obj(clsTest());
clsTest obj1 = clsTest();
clsTest obj2 = clsTest(obj1);
return 0;
}
+ 1
No, the first constructor is w/o any parameter. That's what for obj1 is called
public:
clsTest() {cout << "Constructor\n";};
+ 1
It's called Copy Elision
C++17 will directly call the constructor onto the final location (even if there where side effects) instead of moving it
(prvalues can't be moved anymore, they have to be first materialized into xvalues)
Also, the way you wrote it is actually a function declaration
+ 1
Ketan Lalcheta
clsTest obj; // this calls the default constructor
clsTest obj(); // this is a function
clsTest func()
{
...
}
clsTest obj{clsTest()}; // this would have been a copy constructor from a default constructor (if it wasn't for Copy Elision)
clsTest obj(clsTest()); // this is a function
clsTest func(clsTest (*f)())
{
...
}
clsTest obj1;
clsTest obj2(obj1); // this is a copy constructor
clsTest obj3{ob1}; // this too is a copy conctructor
+ 1
Oh ok ... Thanks Angelo
I got your point ...
If (obj1) and {obj1} both are copy constructor, then (clsTest) and {clsTest} must atleast call constructor due to copy elision...
I understand clsTest() should not be argument to constructor and hence I thought to just pass clsTest without bracket...
clsTest obj(clsTest);
Still it is not calling any of constructor... So getting confused that how object is created?
+ 1
Ketan Lalcheta
clsTest obj(clsTest);
is also a function
clsTest obj(clsTest arg)
{
...
}
+ 1
Ketan Lalcheta
In C++ you can initialize in 3 ways:
1) (...)
2) {...}
3) = ...
They are equivalent, but the first way can be ambiguous, and be mistaken for a function declaration
Regarding initialization from temporary objects:
Before C++11 you had to pass them by value
(edit: or const lvalue reference)
And then C++11 introduced the move semantics
Another useful example is:
clsTest obj(clsTest{});
In C++11 this would call the default constructor for clsTest{} and than the move constructor for obj
In C++17 there is copy elision, and the default constructor is called directly for obj
+ 1
First thing first... Appreciate your help and effort
Doubt related to temp object before c++11...
How to pass temp object as value to copy constructor ? Copy constructor can't be call by value... That's why it was const reference
Do you mean to say that
clsTest obj(clsTest{}) is valid before c++ 11 as well ? This piece of code only compile copy constructor has const reference.. if we define own copy constructor and it is just a reference, not const reference, code will not compile...
So clsTest obj(clsTest{}) is valid if C++11 prior version is there?
+ 1
Sorry, my fault
What I meant was that before C++11 there weren't rvalue references, so to pass a temp to a function you had to do it by value and I forgot to say also by const lvalue reference (sorry)
Than, everything you said is right
0
In your example code , obj2 works properly and calls for copy constructor...
Again wondering for obj1. It just calls constructor... Isn't it should call constructor and move assignment?
0
Angelo okay... Even if it will optimize due to elision, atleast one constructor call hasn't be there ?
Also could you please explain what do you mean by function declaration way I wrote
0
Okay ... One last question...
I belive {} sysntax came into picture when common initialisation is introduced in latest standards of C++... Is it correct or not ? Seems it's not correct otherwise earlier version of c++ would not have a temporary object in argument of object creation...
So , is it ok to say that {} is available before C++11 also for temporary object passed as argument to play role of initialisation?
0
No problem...many thanks for your help...