hi all,
In the book "Programming Languages-Pragmatics" second edition by Michael l. Scott and Morgan Kaufmann, there is a sentence while explaining about copy constructor:
"In recognition of this intent, a single-argument constructor in C++ is called a copy constructor."
This is about the constructor of the form foo::foo(const foo& object);
I could understand about that constructor but couldn't understand the statement I have quoted above...I think we cannot state that every single-argument constructor is a copy constructor....If I'm wrong or I seems to be misunderstood the sentence...please help me....
Copy constructor: foo::foo(const foo& object);
Normal single argument constructor: foo:foo(int a);
Both are single argument constructors but only the first one is a copy constructor...
Thanks in advance....

Recommended Answers

All 4 Replies

It is called a copy constructor because it takes a reference to an object of its own type and creats a new object that is a copy of the object passed into the constructor. take this as an example

std::string foo("foobar");  //uses single argument constructor
std::string bar(foo);  // uses the copy constructor to copy the contents of foo into the new object called bar.

"In recognition of this intent, a single-argument constructor in C++ is called a copy constructor."

That statement is false. Here is the standard definition, from Section 12.8/2:

A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6).

Then, all constructors that can be called with a single argument are called (explicit) converting constructors ("explicit" only if marked with the attribute explicit). The copy-constructor (and move-constructor) is a special kind of converting constructor.

So, the authors of the book were wrong, you were right.

Thanks for the clarification.....
I think I can make it as solved, can't I?

I think I can make it as solved, can't I?

You're the OP on this thread, you can mark it as solved whenever you are satisfied with the answers. You don't have to ask permission.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.