I have a problem with understanding what happens when you return an object of a class ( Not a specific class ) form a function ( pass by value ) in this code : EXAMPLE 1
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class test {
public:
test(int y) {
printf(" test(int y)\n");
}
test() {
printf(" test()\n");
}
test( const test& z) {
printf(" test( const test&z)\n");
}
test(test&& s)noexcept{
printf(" test(test&& s)\n");
}
test& operator=(test e) {
printf(" test& operator=( test e)\n");
return *this;
}
};
test Some_thing() {
test i;
return i;
}
int main()
{
Some_thing();
return 0;
}
The Output :
test()
test(test&& s)
The previous Output makes me understand that in the function ( Some_thing ( ) ) Scope there are two objects are created . the first one is an lvalue object which we create it in the first line in the function ( Some_thing ( ) ) and we give it a name ( i ) So the constructor test ( )
is called.
And the second one is an rvalue object So the constructor test ( test&& s )
is called.
But when i deleted this constructor test(test&& s)noexcept
and changed this constructor
test( const test& z)
into
test( test& z)
and run the code again :
EXAMPLE 2
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class test {
public:
test(int y) {
printf(" test(int y)\n");
}
test() {
printf(" test()\n");
}
test( test& z) {
printf(" test( test&z)\n");
}
test& operator=(test e) {
printf(" test& operator=( test e)\n");
return *this;
}
};
test Some_thing() {
test i;
return i;
}
int main()
{
Some_thing();
return 0;
}
The Output :
test()
test( test&z)
While I expected that this code will not compile because there is no constructor takes test&&
or const test&
as a parameter
and when i tried to add one line to the previous code which is test(test&& z) = delete
EXAMPLE 3
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class test {
public:
test(test&& z) = delete;
test(int y) {
printf(" test(int y)\n");
}
test() {
printf(" test()\n");
}
test( const test& z) {
printf(" test( test&z)\n");
}
test& operator=(test e) {
printf(" test& operator=( test e)\n");
return *this;
}
};
test Some_thing() {
test i;
return i;
}
int main()
{
Some_thing();
return 0;
}
I tried to compile it but it does not compile and it does not run
So how does EXAMPLE 2 compile and run ?????? and how can the constructor test( test&z)
be used instead of
test(test&& z)
??????
( I mean test( test&z)
is not test( const test&z)
So test( test&z)
can not be used instead of test(test&& z)
)
edit : this code compiles and runs : EXAMPLE 4
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class test {
public:
test(test&& z) = delete;
test(int y) {
printf(" test(int y)\n");
}
test() {
printf(" test()\n");
}
test(const test& z) {
printf(" test( test&z)\n");
}
test& operator=(test e) {
printf(" test& operator=( test e)\n");
return *this;
}
};
int main()
{
test u;
test r(u);
return 0;
}
The Output :
test()
test( test&z)