unable to differentiate between constructors

I want to know what are the differenecs between the lines commented below.How do they work and how they are different from each other?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  include <iostream>

using namespace std;

class cmplex
{
int a,b;
public:
cmplex(int x,int y){a=x;b=y;cout<<"its inside constructor 1"<<a<<" "<<b<<endl;}
cmplex(int y){a=y;cout<<"its inside constructor 2 "<<a<<endl;}
cmplex(){cout<<"its inside constructor 3"<<endl;}
};
int main()
{
    cmplex c3;//this line
    cmplex c5=cmplex(2,3);//this line
    cmplex c6(2,3);//this line
    c1=cmplex(2,3);//this line
    cmplex c4(c2);//this line
    return 0;
}
Last edited on
You've failed to define assignment and move, so you're not seeing everything.
Your example does not compile.

I did post on your earlier thread a more verbose program: http://www.cplusplus.com/forum/beginner/232754/

Lets copy/adapt it here nevertheless:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using std::cout;

class cmplex
{
  int a,b;
public:
  cmplex(int x,int y) : a(x), b(y) {cout<<"ctor A "<<a<<' '<<b<<'\n';}
  cmplex(int y) : a(y), b(42) {cout<<"ctor B "<<a<<' '<<b<<'\n';}
  cmplex() : a(7), b(13) {cout<<"ctor C "<<a<<' '<<b<<'\n';}
  cmplex(const cmplex &c) : a(c.a), b(c.b) {cout<<"copy ctor "<<a<<' '<<b<<'\n';}
  cmplex& operator=(const cmplex &c)
    { a=c.a; b=c.b; cout<<"copy assi "<<a<<' '<<b<<'\n'; return *this;}
  cmplex(cmplex &&c) : a(c.a), b(c.b) {cout<<"move ctor "<<a<<' '<<b<<'\n';}
  cmplex& operator=(cmplex &&c)
    { a=c.a; b=c.b; cout<<"move assi "<<a<<' '<<b<<'\n'; return *this;}
};

int main()
{
    cout << "c3\n";
    cmplex c3;
    cout << "c5\n";
    cmplex c5=cmplex(2,3);
    cout << "c6\n";
    cmplex c6(4,5);
    cout << "c3=\n";
    c3=cmplex(6,7);
    cout << "c4\n";
    cmplex c4(c5);
    cout << "c7\n";
    cmplex c7 = c5;
    cout << "c8\n";
    cmplex c8{c5};
    cout << "c9\n";
    cmplex c9( cmplex(8,9) );
    cout << "c9=\n";
    c9 = c7;
    cout << "c6=\n";
    c6 = std::move(c7);

    return 0;
}

c3
ctor C 7 13
c5
ctor A 2 3
c6
ctor A 4 5
c3=
ctor A 6 7
move assi 6 7
c4
copy ctor 2 3
c7
copy ctor 2 3
c8
copy ctor 2 3
c9
ctor A 8 9
c9=
copy assi 2 3
c6=
move assi 2 3

Now it is your turn to explain differences, if any.
Last edited on
How does cmplex c1=cmplex(2,3) creates temporary object here?..please give me detailed explaination
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
     
using namespace std;
     
class cmplex
{
int a,b;
public:
cmplex(cmplex &c){a=c.a;b=c.b;cout<<"its inside copy constructor 1"<<endl;cout<<a<<b<<endl;}
};
int main()
{
cmplex c1=cmplex(2,3);
return 0;
}
We cannot possibly know whether it does or does not, because your code does not compile.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
     
using namespace std;
     
class cmplex
{
int a,b;
public:
cmplex(int x,int y){a=x;b=y;cout<<"its inside constructor 1"<<a<<" "<<b<<endl;}
cmplex(cmplex &c){a=c.a;b=c.b;cout<<"its inside copy constructor 1"<<endl;cout<<a<<b<<endl;}
};
int main()
{
cmplex c1=cmplex(2,3);
return 0;
}

this code does not run,as at line no 9 i have to add cmplex(const cmplex &c) instead of cmplex(cmplex &c)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
     
using namespace std;
     
class cmplex
{
int a,b;
public:
cmplex(int x,int y){a=x;b=y;cout<<"its inside constructor 1"<<a<<" "<<b<<endl;}
cmplex(const cmplex &c){a=c.a;b=c.b;cout<<"its inside copy constructor 1"<<endl;cout<<a<<b<<endl;}
};
int main()
{
cmplex c1=cmplex(2,3);
return 0;
}

this code runs
but look at this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
     
using namespace std;
     
class cmplex
{
int a,b;
public:
cmplex(int x,int y){a=x;b=y;cout<<"its inside constructor 1"<<a<<" "<<b<<endl;}
cmplex(cmplex &c){a=c.a;b=c.b;cout<<"its inside copy constructor 1"<<endl;cout<<a<<b<<endl;}
};
int main()
{
cmplex c1(2,3);
return 0;
}

this code runs without adding const. why is this happening. I am only using cmplex c1(2,3) instead of cmplex c1=cmplex(2,3) at line 13.My question is, "Is somehow cmplex c1=cmplex(2,3) is creating a temporary object or something else"?.
Last edited on
Look at my previous example program and its output.

What is the output of these:
1
2
3
cmplex c5 = cmplex( 2, 3 );
cmplex c6( 4, 5 );
cmplex c9( cmplex( 8, 9 ) );


Which members were called?
Were there any lines that invoke more than one member? If yes, which situation?


Here is an another test set:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    cmplex c0( 0 );
    cmplex c1{ 1 };
    cmplex c2{ c1 };
    cmplex c3 = 3;
    cmplex c4 = cmplex{ 4 };
    cmplex c5( cmplex{ 5 } );
    cmplex c6{ cmplex{ 6 } };
    cmplex c7 = cmplex( 7 );
    cmplex c8 = static_cast<cmplex>( 8 );
    cmplex c9 = (cmplex)9;
}


Here are some versions of the interface of the cmplex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// A
cmplex( int );
cmplex( const cmplex & );
cmplex( cmplex && );

// B
cmplex( int );
cmplex( const cmplex & );
// cmplex( cmplex && );

// C
cmplex( int );
// cmplex( const cmplex & );
cmplex( cmplex && );

// D
cmplex( int );
cmplex( cmplex & );
cmplex( cmplex && );

// E
cmplex( int );
cmplex( cmplex & );
// cmplex( cmplex && ); 


The A, B, and D do compile and produce:
ctor B 0 42
ctor B 1 42
copy ctor 1 42
ctor B 3 42
ctor B 4 42
ctor B 5 42
ctor B 6 42
ctor B 7 42
ctor B 8 42
ctor B 9 42


The C compile fails only on cmplex c2{ c1 }; with:
In function 'int main()':
error: use of deleted function 'constexpr cmplex::cmplex(const cmplex&)'
note: 'constexpr cmplex::cmplex(const cmplex&)' is implicitly declared as deleted because 'cmplex' declares a move constructor or move assignment operator


The E compile fails for c3-c9.


* The cmplex c2{ c1 }; clearly requires a copy constructor.
It does not care whether constructor takes const or non-const reference, because c1 is not a temporary.

* The compiler does provide a copy constructor cmplex::cmplex(const cmplex&) (unless prevented). Note the const.

* All the other lines seem to call cmplex::cmplex(int) and nothing else. However, if there is
only cmplex::cmplex(cmplex &) and neither cmplex::cmplex(const cmplex&) nor complex::cmplex( cmplex && ); available, then they fail to compile.


Is there a temporary? Yes and no.
Formally either copy construction or move construction from temporary is used for c3-c9, but it is optimized out.
Last edited on
This helped me a lot,Really thanks from the core of my heart for clearifying the fact.It helped me so much.Thanks for your help again
Topic archived. No new replies allowed.