Role of copy constructor

D

Dhirendra Singh

Hi,
In the below example, does copy constructor has any role in the
initialization ?

class complex {
private:
double re, im;
public:
complex( double r = 0, double i = 0) :re(r), im(i) {}
complex( const complex& c) :re(c.re), im(c.im) {}
};

int main()
{
complex x = complex(3,2);
return 0;
}
 
V

Victor Bazarov

Dhirendra said:
In the below example, does copy constructor has any role in the
initialization ?

class complex {
private:
double re, im;
public:
complex( double r = 0, double i = 0) :re(r), im(i) {}
complex( const complex& c) :re(c.re), im(c.im) {}
};

int main()
{
complex x = complex(3,2);
return 0;
}

Yes, it does. The language requires it to be accessible _as_if_ it
were used; however, it can be optimized away. Make your copy c-tor
private and try compiling again.

V
 
R

rory.brandybuck

It depends on the compiler you use.
For example belowed compiles without errors on Intel(R) C++ Compiler
v7.1:

#include <iostream>

namespace {
using std::cout;
using std::endl;

class complex {
private:
double re, im;
public:
complex( double r = 0, double i = 0) :re(r), im(i) {
cout << "complex()" << endl;
}
private:
complex( const complex& c) :re(c.re), im(c.im) {
cout << "complex(complex&)" << endl;
}

};

}
int main()
{
complex x = complex(3,2);
}
 
R

rory.brandybuck

It depends on the compiler you use.
For example, the belowed code compiles without any errors on Intel(R)
C++ Compiler
v7.1:

#include <iostream>

namespace {
using std::cout;
using std::endl;

class complex {
private:
double re, im;
public:
complex( double r = 0, double i = 0) :re(r), im(i) {
cout << "complex()" << endl;
}
private:
complex( const complex& c) :re(c.re), im(c.im) {
cout << "complex(complex&)" << endl;
}
};
}

int main()
{
complex x = complex(3,2);
}
 
V

Victor Bazarov

It depends on the compiler you use.
For example belowed compiles without errors on Intel(R) C++ Compiler
v7.1:

#include <iostream>

namespace {
using std::cout;
using std::endl;

class complex {
private:
double re, im;
public:
complex( double r = 0, double i = 0) :re(r), im(i) {
cout << "complex()" << endl;
}
private:
complex( const complex& c) :re(c.re), im(c.im) {
cout << "complex(complex&)" << endl;
}

};

}
int main()
{
complex x = complex(3,2);
}

It does? Then it's a bug in the compiler. The code is ill-formed. Try
it with Comeau online, and you'll see what _ought_ to be.

Are you sure you didn't use some kind of "Microsoft compatibility mode"
instead of "normal strict standard C++ rules mode" when you compiled it?
Some compilers are known to let ill-formed code to go through simply
because they want to be popular among sloppy programmers. In my book
it's no good, but folks are often happy that at least well-formed code
compiles. It's up to the users to decide which product is acceptable.


V
 
D

Dhirendra Singh

when it is never called then why compiler cries when copy constructor
is declared as private ?
complex x = complex(3,2) is always treated like complex x(3,2)
I am reading the stroutstrap book and did not find anything mentioned
about it.
 
V

Victor Bazarov

Dhirendra said:
when it is never called then why compiler cries when copy constructor
is declared as private ?

Because the Standard requires it to.
complex x = complex(3,2) is always treated like complex x(3,2)
I am reading the stroutstrap book and did not find anything mentioned
about it.

The statement

complex x = complex(3,2);

is *semantically* equivalent to

complex _some_temporary_value(3,2);
complex x(_some_temporary_value);

The Standard _allows_ to forgo creation of the temporary and generate
code that would be the same as writing

complex x(3,2);

*HOWEVER* the copy constructor _shall_ be available *as if* it is used.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top