How to create an object such that it should not call constructor by default.

Showing Answers 1 - 17 of 17 Answers

Yugandhar Tikhe

  • Oct 3rd, 2006
 

Make your constructor private or protected.

  Was this answer useful?  Yes

lakshminarsu

  • Oct 16th, 2006
 

//The following code will not call the constructor when creating the object of class Base

class Base

{

public:

static Base* Instantiate()

{

return (new Base);

}

//What ever members you want to add

void Print()

{

cout<<"Print Successfully"<<endl;

}

private:

Base()

{

cout<<endl<<"Object Created Successfully";

}

};

int main(int argc, _TCHAR* argv[])

{

Base *ptr=NULL;

ptr = (Base*)malloc(sizeof(Base));

ptr->Print();

return 0;

}

Saneesh Joseph

  • Oct 24th, 2006
 

It is impossible. Declaring a contructor private is not recommended/illegel to C++ rules. Constructor should be declared as public.

  Was this answer useful?  Yes

Adam Larson

  • Nov 1st, 2006
 

Constructors can be private it is not illegal!The point of making a constructor private is to limit the number of instances of that class. I use this quite often when creating singleton classes. I have a logger that I don't want there to be more than one copy so I create a function like this:static Logger* Instance(){static Logger instance;return &instance;}Then to access any of the Loggers functions just do this:Logger::Instance()->WriteToFile("Hello World");Note the static function needs to be implemented in a cpp file or it will not work properly.

  Was this answer useful?  Yes

Renjith SA

  • Nov 10th, 2006
 

Virtual functions implies that subclasses might handle those functions, OK.

class base

{

virtual base() //implies derived class might handle it

{

}

};

class deri_1 : public base

{

};

void main(void)

{

deri_1 obj;

at this point compiler tries to initialize deri_1 object.  At this point, compiler notices that deri_1 has a parent, compiler tries to initilize base.  Since base has a virtual ctor, it jerks down to derived class before initializing base class, and tries to initialize derived class (deri_1).  Since base is not initialized from deri_1, it will jump back to base and this process repeats and will fail for ever.  So in no way we have virtual constuctors.

Comments recommented.

  Was this answer useful?  Yes

manoj kumar

  • Dec 14th, 2006
 

1.Why constructor can't be virtual?The responsibility of constructor is to initialize the variable member as well as to build the virtual function table (VTABLE) and initialize the virtual pointer (VPTR) and associate the VPTR with each object, this task is done at the time of object creation. If the constructor are virtual then who will do the task. This is the exact reason that constructors are not virtual.2.What is the difference between destructor and virtual destructors?There is a significant difference between virtual destructor and destructor. Ask any programmer, he'll immediately reply saying "A destructor is a member function of a class, which gets called when the object goes out of scope". This means all clean ups and final steps of class destruction are to be done in destructor. A virtual function is something which helps a derived class in overriding the implementation of a functionality of a base class.The order of execution of destructor in an inherited class during a clean up is like this.1. Derived class destructor2. Base class destructorA difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed.Now turning our attention to why a destructor has to be virtual, the reason is that we, programmers are very smart. We'll do days and nights of work to inherit and extend the functionality of an existing class which is being used, and say that we don't want to change the implementation/interface just for the sake of a new entrant. Let me explain this with an example.#include class Base{ public: Base(){ cout<<"Constructor: Base"< };void main(){ Base *Var = new Derived(); delete Var;}Try executing this code, you'll see the difference. To our observation, the constructors are getting called in the proper order. But to the dread of a programmer of a large project, the destructor of the derived class was not called at all.This is where the virtual mechanism comes into our rescue. By making the Base class Destructor virtual, both the destructors will be called in order. The following is the corrected sample.#include class Base{ public: Base(){ cout<<"Constructor: Base"<

  Was this answer useful?  Yes

dasam

  • Mar 29th, 2007
 

Adam is absolutely correct.

It is not illegal to make the constructor as private. We generally do this while creating the singleton class patterns.

  Was this answer useful?  Yes

Indrajit

  • Jul 22nd, 2007
 

Virsa's questions, why constuctors cannot be private and the difference between... are not related to the original question at all, and I simply don't understand why people even take the pain of answering a separate question in the context of "how to create an object so that it should not call constructor by default?".

One answer to the original question could be to use "operator new" for creation of the object. "operator new" only allocates space for the object and does not call the constructor unlike the "new" operator which does both.

Regards

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions