answersLogoWhite

0

What is explicit copy constructor?

Updated: 8/10/2023
User Avatar

Wiki User

7y ago

Best Answer

An explicit constructor is one that is declared explicit and thus prevents implicit construction of an object. To understand explicit construction you must first understand implicit construction.

Consider the following code that uses a simple class, Square, to compute the square of any given integer:

#include

using namespace std;

class Square

{

private:

int m_number;

public:

Square(int number):m_number(number*number){}

friend void display(Square square);

};

void display(Square square)

{

cout << "Number = " << square.m_number << endl;

}

int main()

{

Square ten(10); // Explicit construction

Square twenty=20; // Implicit construction

display(ten);

display(twenty);

display(30); // What does this mean?

return(0);

}

Output:

Number = 100

Number = 400

Number = 900

Although there's nothing intrinsically wrong with the line display(30), it's not exactly clear to the reader what this line does. The display() function clearly expects an instance of a Square object, yet we're passing a literal constant.

The clue is the implicit constructor call, Square twenty = 20. Although we declared no assignment operator that accepts a literal constant, the statement implies we call the constructor, Square(int). In other words, the line is equivalent to explicitly calling Square twenty(20).

This is really no different to the way primitives work. That is, int x=5 is the same as calling int x(5), but the former is a more natural form of assignment. However, this behaviour is not always desirable. While Square twenty=5 may be fine, the unwanted side effect is that it also allows us to make ambiguous calls, like display(30).

Although the line display(30) forces the display()function to construct a local Square object using implicit construction, it's not obvious to the reader that an object is created (and subsequently destroyed), and as a result of this it isn't obvious to the reader why display(30) would produce the result Number = 900.

Note that while this usage may well be obvious to the developer while writing the code, when the developer revisits the code later, it may not be quite so obvious. And if the developer has trouble reading their own code, you can imagine how difficult it would be for a third party to understand the same code.

To avoid this problem, we must declare the constructor to be explicit:

explicit Square( int number ):m_number(number*number){}

As soon as we do this, the lines Square twenty = 20 and display(30) become invalid. To rectify this, we must replace them with calls to the explicit constructor instead:

int main()

{

Square ten(10); // Explicit constructor

Square twenty(20); // Explicit constructor

display(ten);

display(twenty);

display(Square(30)); // Explicit constructor

return(0);

}

Now it is much clearer to the reader what's going on. Although we've lost the ability to implicitly construct a Squareobject, we've assured our code remains readable at all times by enlisting the help of the compiler to ensure that we never unintentionally construct a Square object via implication.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

There is no such thing as an explicit copy constructor. Explicit constructors are required when we wish to prevent narrowing through implicit conversion of argument values, but a copy constructor's argument is a constant reference to an existing object of the same type, thus there is no implicit conversion and therefore no narrowing. That is, copying an object of type T or of a type derived from type T will always create a new object of type T. If we want to copy a type derived from a T, then we need to invoke that derived type's copy constructor, not the copy constructor for a T.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

It means that it is explicitly written out - as opposed to the implicit constructor, which is created automatically in some programming languages, when you don't provide one.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is explicit copy constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


Does Java support copy constructor?

No. Java does not support copy constructor


What is the difference between implicit and explicit Java programming?

Explicit means done by the programmer. Implicit means done by the JVM or the tool , not the Programmer. For Example: Java will provide us default constructor implicitly.Even if the programmer didn't write code for constructor, he can call default constructor. Explicit is opposite to this , ie. programmer has to write .


What is the prototype of a copy constructor for a class X?

class X { public: X(); // default constructor X(const X&amp; x); // copy constructor // ... }; int main(void) { X objx1; //normal ctor X objx2 = x1; // copy ctor X x3(x2); // copy ctor }


Why you use constructor chaining?

Constructor ChainingWe know that constructors are invoked at runtime when you say new on some class type as follows:Lamborghini h = new Lamborghini();But what really happens when you say new Lamborghini() ? (Assume Lamborghini extends Car and Car extends Object.)1. Lamborghini constructor is invoked. Every constructor invokes the constructor of its superclass with an (implicit) call to super(),2. Car constructor is invoked (Car is the superclass of Lamborghini).3. Object constructor is invoked (Object is the ultimate superclass of all classes, so class Car extends Object even though you don't actually type "extends Object" into the Car class declaration. It's implicit.) At this point we're on the top of the hierarchy.4. Object instance variables are given their explicit values. By explicit values, we mean values that are assigned at the time the variables are declared, like "int x = 27", where "27" is the explicit value (as opposed to the default value) of the instance variable.5. Object constructor completes.6. Car instance variables are given their explicit values (if any).7. Car constructor completes.8. Lamborghini instance variables are given their explicit values (if any).9. Lamborghini constructor completes.

Related questions

What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


Does Java support copy constructor?

No. Java does not support copy constructor


Discrbe breifly about Copy constructor overloading?

A copy constructor usually refers to a constructor which takes an object, and returns a copy of that object. I can think of no way to overload the constructor without changing its functionality.


What is the advantage of user-defined copy constructor?

What is the advantage of user-defined copy constructor


What is the difference between implicit and explicit Java programming?

Explicit means done by the programmer. Implicit means done by the JVM or the tool , not the Programmer. For Example: Java will provide us default constructor implicitly.Even if the programmer didn't write code for constructor, he can call default constructor. Explicit is opposite to this , ie. programmer has to write .


How do you declare constructor in cpp?

More or less as you would any other function, except there is no return type (not even void) and an initialisation list can be placed between the declaration and the definition that follows it. The initialisation list allows your constructor to call base class constructors besides the default constructor as well as initialise member variables according to the parameters passed to your constructor. The constructor's name must be the name of the class. Note that if you don't declare any constructors, the compiler generates both a default and copy constructor. If any constructor is declared you lose the default constructor unless you declare one yourself. The copy constructor is always present but must be overridden if your class contains pointers to memory allocated to the class itself. If you don't, the compiler generated copy constructor will perform a member-wise copy of the member variables, resulting in a shallow copy of the pointers themselves, rather than a deep copy of the memory they point to. The copy constructor must accept a constant reference to the same class of object.


What is the prototype of a copy constructor for a class X?

class X { public: X(); // default constructor X(const X&amp; x); // copy constructor // ... }; int main(void) { X objx1; //normal ctor X objx2 = x1; // copy ctor X x3(x2); // copy ctor }


What is copy constructor in java?

Java, unlike C++ does not support copy constructors.


What is the meaning of copy constructor?

The meaning of copy constructor is a special programmer of the C++ language that copys existing coding projects and enhances or makes them better projects.


What is overloaded constructor in oop?

Overloading a function simply means providing the same function name with different argument types. Class constructors are no different. In fact, even if you declare no constructors in a class, there will be two compiler-generated constructor overloads provided for you: a default constructor; and a copy constructor. If you declare any other constructors, the compiler-generated default constructor will no longer be generated. You must declare your own default constructor if you require one. The copy constructor is always generated, however the default implementation only performs a member-wise copy of the class members. If your class contains a pointer to allocated memory you must provide your own copy constructor to perform a deep-copy of those pointers, so each instances "owns" its own copy of the memory.


Why you use constructor chaining?

Constructor ChainingWe know that constructors are invoked at runtime when you say new on some class type as follows:Lamborghini h = new Lamborghini();But what really happens when you say new Lamborghini() ? (Assume Lamborghini extends Car and Car extends Object.)1. Lamborghini constructor is invoked. Every constructor invokes the constructor of its superclass with an (implicit) call to super(),2. Car constructor is invoked (Car is the superclass of Lamborghini).3. Object constructor is invoked (Object is the ultimate superclass of all classes, so class Car extends Object even though you don't actually type "extends Object" into the Car class declaration. It's implicit.) At this point we're on the top of the hierarchy.4. Object instance variables are given their explicit values. By explicit values, we mean values that are assigned at the time the variables are declared, like "int x = 27", where "27" is the explicit value (as opposed to the default value) of the instance variable.5. Object constructor completes.6. Car instance variables are given their explicit values (if any).7. Car constructor completes.8. Lamborghini instance variables are given their explicit values (if any).9. Lamborghini constructor completes.


Can the parameter of the copy constructor be passed by value?

No. The parameter of the copy constructor is always passed by reference, specifically, a const reference to the class.Think about it. If it were passed by value, then the compiler would already know how to copy the object into the formal parameter, but the purpose of the copy constructor is to provide the code to copy the object, so its kind of a "cart before the horse" thing to think about call by value here.