469,612 Members | 2,671 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Copy Constructor

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
Jul 22 '05 #1
5 2277
On Sat, 10 Jan 2004 05:17:51 -0800, LRS Kumar wrote:
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

Jul 22 '05 #2
"LRS Kumar" <lr******@yahoo.co.in> wrote in message
news:d0**************************@posting.google.c om...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote...
"LRS Kumar" <lr******@yahoo.co.in> wrote in message
news:d0**************************@posting.google.c om...
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.


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
Jul 22 '05 #4

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:k8*******************@twister.nyroc.rr.com...
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.
Jul 22 '05 #5

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:k8*******************@twister.nyroc.rr.com...
"LRS Kumar" <lr******@yahoo.co.in> wrote in message
news:d0**************************@posting.google.c om...
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?


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.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Edward Diener | last post: by
15 posts views Thread by A | last post: by
8 posts views Thread by Jesper | last post: by
10 posts views Thread by utab | last post: by
8 posts views Thread by shuisheng | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.