P
Prasoon
Can constructors be explicitly called in a program with using
placement new???
As far as I know....considering the code snippet
#include<iostream>
class demo
{
public:
demo(){
std::cout<<"Constructor invoked";
}
~demo(){
std::cout<<"Destructor invoked";
}
};
int main()
{
demo d;//Constructor invoked
d.demo();//Compilation error
demo();//Creation of nameless object
d.~demo();//Allowed but may lead to undefined behaviour as the
local object
//d can be destructed twice
}
Is the creation of nameless object in the above code an explicit call
to constructor or not???
According to me,its not. To call a constructor explicitly we need to
use Placement new but again it is not recommended to use "Placement
New"
Is there any other way of calling constructors explicitly????
P.S: Not to confuse with the explicit keyword
Prasoon
placement new???
As far as I know....considering the code snippet
#include<iostream>
class demo
{
public:
demo(){
std::cout<<"Constructor invoked";
}
~demo(){
std::cout<<"Destructor invoked";
}
};
int main()
{
demo d;//Constructor invoked
d.demo();//Compilation error
demo();//Creation of nameless object
d.~demo();//Allowed but may lead to undefined behaviour as the
local object
//d can be destructed twice
}
Is the creation of nameless object in the above code an explicit call
to constructor or not???
According to me,its not. To call a constructor explicitly we need to
use Placement new but again it is not recommended to use "Placement
New"
Is there any other way of calling constructors explicitly????
P.S: Not to confuse with the explicit keyword
Prasoon