473,396 Members | 2,070 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,396 software developers and data experts.

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

Similar topics

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...
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...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
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) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.