can we call the constructors manually??

Can we call the constructors manually?or it is always called by the compilers by default?
1
2
3
MyClass a;  // Calls default constructor
MyClass b( a );  // Calls copy constructor
MyClass c( 4, 5 ); // Calls MyClass::MyClass( int, int ) 


etc etc
closed account (S6k9GNh0)
This automatically calls the class constructor. It can be better demonstrated in the new operator:
 
MyClass * bob = new MyClass();
You can call a constructor manually with placement new. I believe this site has an article in the reference section about placement new, but I'm too lazy to dig up the exact link.

Anyway -- you generally don't use placement new unless you're overriding the default new operator. 99.99% of the time you want to let the compiler handle ctor/dtor calling --- there's little reason to do it manually. Only time you should do it manually is if you're doing some kind of advanced memory allocation system (like a memory pool or somesuch).

So basically -- no. The compiler handles ctor/dtor calling automagically. Don't worry about it.

But technically -- yes you can manually call the ctor if it's really necessary. Though the circumstances for when you should do this are extremely limited.
Topic archived. No new replies allowed.