answersLogoWhite

0

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

15y ago

What else can I help you with?

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.


What is the purpose of C plus plus?

Computer programming.


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.


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