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....

Dani AI

Generated

Short answer: no — not every single-argument constructor is a copy constructor. The C++ standard reserves the name "copy constructor" for a specific form of constructor that takes an object of the same class by reference (possibly cv-qualified) as its first parameter and has no other non-defaulted parameters. That makes a copy constructor a special case, not the generic single-argument case. See the canonical description on cppreference: copy constructor. ()

A single-argument constructor that is not declared explicit is called a converting constructor: it defines an implicit conversion from the parameter type to the class type and participates in copy-initialization and overload resolution. Marking such a constructor explicit disables those implicit conversions. Both points are documented here: converting constructors and the explicit specifier. These distinctions are the practical reason was correct and why 's example illustrated different behavior for copying versus converting. (en.cppreference.com)

Practical guideline and example (use the compiler-generated copy behavior unless a custom copy is needed). Single-arg constructors that are intended to allow implicit conversion should be deliberately non-explicit; otherwise prefer explicit to avoid surprising conversions. Small illustration:

class Widget {
public:
    Widget(int n);            // converting constructor (implicit if not 'explicit')
    explicit Widget(double d); // explicit converting constructor
    // copy behavior provided by the compiler unless a custom copy ctor is declared
};

Widget a(42);
Widget b = a;   // copy-initialization -> uses copy constructor (generated if not user-provided)
Widget c = 3.14; // only allowed if corresponding ctor is non-explicit

Note: since C++11 the language also has move constructors and compilers often generate copy/move ctors automatically; be aware of the Rule of Three/Five and copy-elision subtleties. See move constructor and copy constructor. (cppreference.com)

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.