Copy Constructor

L

LRS Kumar

Stoustrup, in The C++ Programming Language, has the following example
in 11.7:

string g (string arg) // string passed by value using copy
constructor
{
return arg; //string returned (using copy constructor)
}

int main()
{
string s = "Newton"; //string initialized (using copy constructor)
s = g(s);
}

The first two comments are clear. I was wondering why the third
comment says that the initialization uses the copy constructor.

If that was the case, wouldn't the following code also have been
initialized using the copy constructor?


#include <iostream>

class X {
int i;
public:
X() { i=0; std::cout << "defcon\n";}
X(int n) {i=n; std::cout << "intcon\n";}
X(const X& x) {i = x.i; std::cout << "copycon\n";}
};

int main()
{
X x = 2;
}

Instead, the output is: intcon


LRSK
 
M

Martijn Lievaart

Stoustrup, in The C++ Programming Language, has the following example
in 11.7:

string g (string arg) // string passed by value using copy
constructor
{
return arg; //string returned (using copy constructor)
}

int main()
{
string s = "Newton"; //string initialized (using copy constructor)
s = g(s);
}

The first two comments are clear. I was wondering why the third
comment says that the initialization uses the copy constructor.

If that was the case, wouldn't the following code also have been
initialized using the copy constructor?


#include <iostream>

class X {
int i;
public:
X() { i=0; std::cout << "defcon\n";}
X(int n) {i=n; std::cout << "intcon\n";}
X(const X& x) {i = x.i; std::cout << "copycon\n";}
};

int main()
{
X x = 2;
}

Instead, the output is: intcon

The copy constructor is *conceptually* used here. So it must exist and be
accessible. IIRC the compiler is allowed to optimize it away though.

HTH,
M4
 
C

Cy Edmunds

LRS Kumar said:
Stoustrup, in The C++ Programming Language, has the following example
in 11.7:

string g (string arg) // string passed by value using copy
constructor
{
return arg; //string returned (using copy constructor)
}

int main()
{
string s = "Newton"; //string initialized (using copy constructor)
s = g(s);
}

The first two comments are clear. I was wondering why the third
comment says that the initialization uses the copy constructor.

If that was the case, wouldn't the following code also have been
initialized using the copy constructor?


#include <iostream>

class X {
int i;
public:
X() { i=0; std::cout << "defcon\n";}
X(int n) {i=n; std::cout << "intcon\n";}
X(const X& x) {i = x.i; std::cout << "copycon\n";}
};

int main()
{
X x = 2;
}

Instead, the output is: intcon


LRSK

You have misquoted the book. Nowhere does BS say that

string s = "Newton";

is an example of a copy constructor. Read it again.
 
V

Victor Bazarov

Cy Edmunds said:
You have misquoted the book. Nowhere does BS say that

string s = "Newton";

is an example of a copy constructor. Read it again.

It's not an example of one, but a copy constructor is _used_
in such initialisation. See 8.5/15 for the explanation of
the semantics. The name for this form is "copy initialisation"
and can be found in 8.5/12.

Even if the copy-construction is optimised (as permitted),
a copy-constructor must be accessible as if no optimisation is
performed.

Victor
 
S

Sumit Rajan

Cy Edmunds said:
You have misquoted the book. Nowhere does BS say that

string s = "Newton";

is an example of a copy constructor. Read it again.


It does appear on my copy of the special edition also. The snippet posted by
the OP is on page 284 under section 11.7 (Essential Operators). The line is:

string s = "Newton"; //string initilialized (using copy constructor)

A few lines below that BS also writes:

"Often one, but not both, of these copy operations can be optimized away."

Regards,
Sumit.
 
J

jeffc

Cy Edmunds said:
You have misquoted the book. Nowhere does BS say that

string s = "Newton";

is an example of a copy constructor. Read it again.

He didn't quote the book as saying it was an "example" of a copy
constructor. You made that up. He said "using" copy constructor.
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top