473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Role of copy constructor

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
6 1591
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: tracy | last post by:
hi, just wonder, can we copy a role then add some new priviledges to the new role. hm.. i means, example; now i have a role named role_a. Then I copy role_a to create role_b. After I created...
2
by: gudia | last post by:
How would I, using a sql script, copy permissions assigned to a user or a role in one or more databases to another user or a role in their respective databases? Help appreciated
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
by: craig | last post by:
I am working on my first .NET development project that involves custom role-based security per the project requirements. This lead to a general design issue this week that really caused us some...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
9
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.