Question:
Explicit call to constructor in C++?
Geetika S
2012-10-13 21:57:46 UTC
How do I explicitly call a parameterized constructor of a class for an object if I've already called the default constructor of the same class before.

Code:
Name N[20];
.
.
.
//Need to put values in Class Name, Object N[0] using constructor with parameters
Four answers:
Paul
2012-10-14 06:16:06 UTC
You can't call the default constructor and then call another constructor like that.



If what you wanted to do was was declare N in one line but construct it later, you can use pointers.



Name* N[20];

...

N[0] = new N(1, 2, 3);



// Don't forget to clear up the memory

delete N[0];





That's the jist of it. This code could be improved by using smart pointers.
husoski
2012-10-14 05:45:57 UTC
You don't. In fact, you don't really call the constructor at all.



C++ insures that just one constructor is called for each class that an object implements. I don't know if "implements" is the right word, but a class can have one or more base classes, and each of those can have base classes, and so on. Each of those gets exactly one constructor call when the object is created.



If you need to repeat code from a constructor, the usual way to do that is to have both the constructor and some other method call a common private method. You should take care to do this only after the constructor has initialized the object to a usable state. So, a constructor with arguments might first initialized the object to default state, then call a private init() method to build up object state using those paramaters.



Then a public reset() method might take a similar set of arguments, and first release resources (delete any pointers created by new, close a stream, terminate a network connection, whatever) and reset the object to just-created state, and THEN call the same private init() to rebuild the object according to the values supplied. This is also the sort of thing that an overloaded assignment operator might do.



By the way, the code to do that "tear down" might have enough in common with the destructor that both might use another common private method.
peteams
2012-10-15 19:04:18 UTC
You should not call a constructor's on objects more than once, but C++ does allow you to do so if you jump through some hoops.



From what you describe you may simply want an Initialize() function that takes the parameters you wanted to pass to the constructor. If the members are cost, then you may need to const_cast<>() them.



If you really can only initialize the objects during construction then you might consider an array of pointers to your object rather than a plain array. That would allow you to pass the correct parameters to the constructor.



If you really must have an array and call a non-default constructor, then you're looking at syntax like:



new (&N[0]) Name(param1,param2);



That uses C++'s placement syntax to 're-allocate' the memory already allocated. But I would strongly advise looking at other solutions!
Don't sue me!
2012-10-14 05:38:28 UTC
I don't think you can do that, but one trick is to add an assignment operator to the class that takes a value of the same class and do the "initialization" there, example:

N[0] = Name("My Name", 25);

The assignment operator would work like copy constructor, taking values from the assignee and assigning them to its members. The assignee is then thrown away, which is kind of a waste.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...