By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,919 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,919 IT Pros & Developers. It's quick & easy.

Role of copy constructor

P: n/a
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;
}

May 13 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Dhirendra Singh wrote:
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
--
Please remove capital As from my address when replying by mail
May 13 '06 #2

P: n/a
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);
}

May 13 '06 #3

P: n/a
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);
}

May 13 '06 #4

P: n/a
ro*************@gmail.com wrote:
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
--
Please remove capital As from my address when replying by mail
May 13 '06 #5

P: n/a
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.

May 13 '06 #6

P: n/a
Dhirendra Singh wrote:
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
--
Please remove capital As from my address when replying by mail
May 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.