answersLogoWhite

0

There is no such thing as a dynamic constructor, but there is dynamic construction.

struct A {};

foo ()

{

A* x = new A; // dynamic construction.

// use x...

delete x;

}

The above instantiates x on the free store (the heap). Note that every call to new must be matched with a call to delete to release the memory resource. If we hadn't deleted x, then x would have fallen from scope when the function returned, but the instance of A would remain in memory and we'd have no way of recovering that memory until the program terminated.

In general it is best not to use dynamic allocation like this as it's all too easy to forget to delete the resource when we're done with it. A better method is to a statically allocated smart pointer instead:

foo ()

{

unique_ptr<A> x; // static allocation

// use x....

}

Smart pointers are overloaded so they behave just as if they were raw pointers so you can use them just as you normally would a raw pointer, but you must not delete them. When the smart pointer falls from scope it will invoke A's destructor automatically, thus we no longer need to remember to delete the pointer. This technique is central to Resource Acquisition Is Initialisation (RAII).

User Avatar

Wiki User

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


What is a dynamic constructor?

dynamic constructor is a way to constructing an object based on the run type of some existing object. it basically uses standard virtual functions/polymorphism


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.


Static vs dynamic binding in c plus plus?

Static binding occurs at compile time. Dynamic binding occurs at runtime.


What do you mean by initialisation of objects in c plus plus?

Initialization of objects means to provide an initial value for the object. This is usually done by the constructor, or it can be done with an assignment statement.


Constructor and destructor invocation in c?

Not possible in C.


Is overriding a dynamic polymorphism in c plus plus or not?

In C++, overriding and function, method, or operator is a different thing than (dynamic) polymorphism, so overriding a polymorphic method is almost entirely possible.