answersLogoWhite

0


Best Answer

The constructor in C++ is a function that runs when an object is created. It is used to initialize the object.

Types of constructors include default constructors (no arguments), copy constructor (one argument of same type as object), conversion constructors (one argument of some other type), and other constructors (all other cases).

If you do not provide a default constructor, the object will not be initialized.

If you do not provide a copy constructor, the compiler will blindly copy the attributes of the old object into the new object whenever a copy is made, such as in a function call with the object as an argument. This may or may not be safe, especially if any of the attributes are pointers, because that creates the situation of two pointers to the same region of memory. In that case, if that region of memory is an object, then when the object is destroyed, so will the pointed to object, and that will leave the original copied object in an invalid state, with its pointers referencing deleted memory.

User Avatar

Wiki User

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

Wiki User

11y ago

There is no such thing as a dynamic constructor in C++.

Perhaps you are referring to virtual construction. Strictly speaking there is no such thing as a virtual constructor in C++ either. But it can be emulated by implementing pure-virtual create() and clone() synonyms which call the appropriate derived class constructors at runtime.

The basic template for virtual construction is as follows:

class base

{

protected: // construction, assignment and destruction:

base(){}

base( const base & b ){}

base& operator=( const base & b ){}

~base(){}

public: // virtual construction:

virtual base * create() const = 0;

virtual base * clone(const base & b) const = 0;

};

class derived : public base

{

public: // construction, assignment and destruction:

derived():base(){}

derived( const derived & d):base( d ){}

derive& operator=( const derived & d ){}

~derived(){}

public: // virtual construction:

virtual base * create() const { return( new derived() ); }

virtual base * clone(const base & b) const { return( new derived( *this )); }

};

The base::create() and base::clone() methods are declared pure-virtual which basically means all classes derived from base must provide a specific implementation for these methods. As can be seen in the derived class declaration, derived::create() instantiates a new default instance of derived via its own default constructor, while derived::clone() instantiates a new instance of derived via its own copy constructor.

The benefit of having these virtual methods is that the base class can now instantiate new instances of its own derivatives without knowing which type of derivative it actually is. The appropriate constructors are called automatically via the virtual table.

For a practical example, consider a linked list. In order to copy the list, the list's copy constructor must call each of the node's copy constructors. In turn, they must each call their own data member's copy constructors. The problem is that the runtime type of the data can never be known at compile time as there's always the possibility the data could be a derivative. The nodes only have knowledge of the datum's base class and it would be impractical to update the node every time you created a new derivative to ensure the correct copy constructor is called.

By ensuring the data has a clone() method, the node doesn't need to know the exact runtime type of its data -- the virtual table takes care of that for you. So long as the data is derived from a known base class (known to the node), your linked list can handle data of any kind, thus extending the usefulness of your linked list class.

For most classes, the clone() method is all you really need. The create() method is optional, but it can be useful wherever you need a default instance of a class. In our linked list example, for instance, the list itself may be derived in which case the create() method would allow the creation of an empty list of the same type as an existing list.

The whole point of virtual construction is that you do not need to know the runtime type of the class you are instantiating at compile time. So long as you have an existing instance of the class, you can instantiate a new instance from it, even if the class is actually a derivative.

Note that you cannot declare create() and clone() as being static methods of a class because static methods can be called even when no instance of the class exists. Thus you can only call the constructors explicitly -- there is no need for static construction methods. Virtual create() and clone() methods are only applicable when you have an instance of the class from which to instantiate a new instance.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the purpose of a C plus plus constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you write own constructor in c plus plus?

Yes.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


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.


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


Why can you not copy the address of a constructor in c plus plus?

You cannot copy constructor addresses because they are not functions. They have no return type and you cannot call them directly, so it makes no sense whatsoever to point at them. They are invoked automatically whenever you instantiate an object of the type, and that's all they will ever need to do. Pointing at them would serve no purpose, so the language simply does not allow it.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.


Why constructor in Java doesn't have any return type?

The constructor of a Java class is not an ordinary method. Its purpose is not to return any value. The purpose of the constructor is to instantiate the class which it does. Since, the purpose of a constructor is only to instantiate and initialize its class and not anything else, it does not have a return type. All it does is creates an object of that class.


What is the purpose of C plus plus?

Computer programming.


Constructor and destructor invocation in c?

Not possible in C.


What is the primary purpose for construction company?

what is the purpose of the construction industry