Calling a Constructor to Re-Initialize Object

Calling a constructor to re-initialize object

Sort of. Given a class A:

A a;
...
a = A();

the last statement is not initialisation, it is assignment, but it probably does what you want.

Calling a constructor to reinitialize variables doesn't seem to work?

Your line Class(); does call the constructor of the class Class, but it calls it in order to create a "temporary object". Since you don't use that temporary object, the line has no useful effect.

Temporary objects (usually) disappear at the end of the expression in which they appear. They're useful for passing as function parameters, or initializing other objects. It's almost never useful to just create one in a statement alone. The language allows it as a valid expression, it's just that for most classes it doesn't do very much.

There is no way in C++ to call a constructor on an object which has already been constructed. The lifecycle of a C++ object is one construction, and one destruction. That's just how it works. If you want to reset an object during its life, you've done the right thing, which is to call a function to reset it. Depending on your class you might not need to write one - the default assignment operator might do exactly what you need. That's when a temporary can come in handy:

Class myObject;
// ... do some stuff to myObject ...

myObject = Class();

This updates myObject with the values from the freshly-constructed temporary. It's not necessarily the most efficient possible code, since it creates a temporary, then copies, then destroys the temporary, rather than just setting the fields to their initial values. But unless your class is huge, it's unlikely that doing all that 1000 times will take a noticeable amount of time.

Another option is just to use a brand new object for each iteration:

int main() {
int counter = 0;
while (counter < 1000) {
Class myObject;
// stuff happens, each iteration has a brand new object
}
}

Note that Class MyClass(); does not define an object of type Class, called MyClass, and construct it with no parameters. It declares a function called MyClass, which takes no parameters and which returns an object of type Class. Presumably in your real code, the constructor has one or more parameters.

Calling a constructor of a member object within the body of the constructor?

The problem is that when C++ begins the execution of the constructor code all the member variables must have been already constructed (what if you for example call a method of Circle before constructing it?).

If immediate construction is a problem then a possible solution is to add to your member a default constructor and the using assignment in the body of the constructor of the containing class.

You can imagine that native types like int or double do have a default constructor, that's why you can initialize them later (note however that for one of the many ugly quirks of the language the default constructor for an int or a double doesn't actually do anything in this case and you're not allowed to do anything with such a member except assigning it a value - for example reading it is not allowed).

You cannot use base(r) in the body because that's not a valid statement... following a name with an opening parenthesis is only used for function call, for initialization in a declaration or for member initialization in the constructor member initialization list.

If you provide Circle with a default constructor then you can do

Cylinder(double r, double h) {
base = Circle(r);
height = h;
}

Note however that the approach of constructing non-working objects to fix them later is not the best approach for C++. The language likes the idea that if an object is constructed then it's usable and deviations from this are considered only when necessary (C++11 drifted away a bit from this original path with move constructor... but that's another story).

Call constructor to initialize variables

In C++, constructor is only called once when the object is constructed, and not anymore in your class method. In your code

void foo::setup(int x, int y) {
foo(); // ==> this line
}

A temporary foo object will be created, which is independent of the current this object, and therefore the a, b and c fields of this object will be unchanged.

In order to do what you have in mind, create a class method, say foo::reset(), and call it from inside foo::setup().

Is a constructor a function and is it possible to call a constructor

Formally in the C++ Standard it is (along with several others) a special member function so yes it is a function, but it is a special function and not all of the normal rules apply.

There is no syntax to write code that calls a constructor directly or forming a function pointer to it. The Standard specifically says "Constructors do not have names."

The compiler will automatically call a constructor when an object is created. The compiler will also automatically call constructors for subobjects (bases and members) of a class object. "Delegating constructors" are sort-of a degenerate case of initialization of subobjects (In formal algebra, we say that any set is a subset of itself, and say "strict" subset when we mean a subset that is not the entire set).

There are a variety of ways to create an object and some of them look like a function call, but that's actually a cast and results in creation of a new object, on which the constructor is called implicitly by the compiler. There's also placement-new syntax which doesn't do very much besides causing the compiler to implicitly call the constructor -- but even there a brand new object is being created.

One important way in which the compiler's implicit call to a constructor differs from an explicit function call found in user code is that the implicit call occurs within an implicit try/catch scope that will result in destruction of subobjects if an exception occurs. A direct call to the constructor, if one were possible, wouldn't have such extra behavior.

how to reinitialize base class in c++

Constructors are meant to create objects. Hence they are used once. You should create a method to do initialization and call that from constructors.

(Re)Initializing object without copy constructor in cpp

Consider using placement new on your object's current memory. This will initialize a new object in currently used memory without allocating new memory. That might be a solution for you. For example:

new (&o) Object(args)

Try this:

#include <iostream>

class A
{
public:

A(int i)
{
m_i = i;
}

int m_i;
};

int main()
{
int s = 10;
int *ps = new (&s) int(100);
std::cout << *ps;
std::cout << s;

A a(5);
new (&a) A(49);
std::cout << a.m_i;
}

g++ -std=c++98 main.cpp returns 10010049, which is correct.



Related Topics



Leave a reply



Submit