After creating a instance of a class, can we invoke the constructor explicitly? For example
class A{ A(int a) { } } A instance; instance.A(2);
Can we do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use placement new, which permits
However, from your example you’d be calling a constructor on an object twice which is very bad practice. Instead I’d recommend you just do
Placement new is usually only used when you need to pre-allocate the memory (e.g. in a custom memory manager) and construct the object later.