answersLogoWhite

0


Best Answer

You cannot. Constructors are specific to the class in which they are declared. They cannot be inherited and so they cannot be virtual.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you create a virtual copy constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


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


Can a constructor be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor. ## Constructor called implicitly not explicitly so constructor is not virtual.


How to create methods in constructor?

You cannot create methods inside a constructor


What is cloning in c plus plus?

Cloning simply means returning a copy of an existing object. When we work with base classes, it helps to include virtual methods that allow us to create new instances of derived objects as well as base objects, even when we don't know the exact type of those derived classes beforehand. We could use runtime information to determine the exact type, but its costly and ultimately defeats the purpose of polymorphism. By convention, the virtual methods are called Create(), Clone() and Copy(). Create() is the same as calling the default constructor of the class. Clone() is the same as calling the copy constructor of the class. Copy() is the same as calling the assignment operator of the class. It should be noted that the actual copy constructor and assignment operators should both call the Copy() method while the Create() method should also be overloaded to cater for all other construction overloads besides the default constructor. With these methods in place, it becomes possible for base classes to implement much of the functionality that would otherwise only be possible by accessing costly runtime information, or by duplicating code in all derived classes, which quickly becomes a maintenance nightmare. The methods can also be used in abstract classes, but they must be declared pure-virtual since you cannot create instances of an abstract class.


Why not sending arefrence to copy constructor will cause an infinite loop?

A copy constructor gets called any time an object needs to be copied. Unlike in some of the newer languages like Java, you can chose to pass objects either by reference or by value. When you pass by reference, only the address of the function is copied. However, if you pass by value, the whole object must be copied. In order to copy the object, the copy constructor will get called. If the copy constructor's parameter is not a reference, then the object will get passed by value. When the object gets passed by value, it needs to get copied so it will call the same copy constructor. Because the object is still being passed by value it'll call itself again to create a copy of itself. The copy constructor will continue to call itself until the stack overflows.


Why you require constructor in cpp?

Constructors are not a requirement of CPP. A default constructor and copy constructor are automatically generated by the compiler for every class you create when no constructors are declared. The only time you need to declare a constructor is when you wish to override the default behaviour of the generated constructors, to ensure the class is correctly initialised. When any constructor is declared, the default constructor is no longer generated by the compiler -- you must define your own default constructor (one that has no parameters, or where all the parameters have default values). The copy constructor is always generated for you regardless of how many other constructors you declare. But if the class contains pointers to allocated memory that is "owned" by the class then you must override the generated copy constructor with your own copy constructor. This is to ensure the memory is deep copied (the generated copy constructor only performs a shallow, member-wise copy of the members). Otherwise two objects of the same class will end up pointing at the same memory, which would be disastrous if either one were to be deleted. The other instance would be automatically invalidated because it would point to memory that was released by the other instance's destructor.


1 Explain the concepts of constructor and destructor Do you have to declare a constructor every time you create a class?

You only need a constructor if the default constructor will not suffice. Often times, it is useful to have a constructor that takes common parameters so that you do not have to write additional code. For example, a Point class might have a constructor for Point(int x, int y), which would be a shortcut for assigning x and y independently. Other classes may not need any default values assigned, and for this, it is acceptable to just use the default constructor. Finally, some classes are virtual, static, or abstract, and so may not need a constructor because the constructor is unnecessary (static), or may be defined elsewhere (virtual, abstract).


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.


Is constractor create object?

No, that would be a constructor. A constructor is involved in creating objects.