views:

179

answers:

5

I wanted to run 1,000 iterations of a program, so set a counter for 1000 in main. I needed to reinitialize various variables after each iteration, and since the class constructor had all the initializations already written out - I decided to call that after each iteration, with the result of each iteration being stored in a variable in main.

However, when I called the constructor, it had no effect...it took me a while to figure out - but it didn't reinitialize anything!

I created a function exactly like the constructor - so the object would have its own version. When I called that, it reinitialized everything as I expected.

int main()
{
 Class MyClass()

 int counter = 0;

 while ( counter < 1000 )
 { stuff happens }

 Class(); // This is how I tried to call the constructor initially.
          // After doing some reading here, I tried:
          // Class::Class(); 
          // - but that didn't work either 
 /* Later I used...
 MyClass.function_like_my_constructor; // this worked perfectly
 */
}

...Could someone try to explain why what I did was wrong, or didn't work, or was silly or what have you? I mean - mentally, I just figured - crap, I can call this constructor and have all this stuff reinitialized. Are constructors (ideally) ONLY called when an object is created?

+1  A: 

Yes, this not typical usage. Create a function that resets your variables, and call the method whenever you need it.

Shawn Simon
A: 

You fell prey to a common misreading of c++. The new c++0x makes things a bit clearer.

The problem is constructions syntax looks like a function call.

void foo( int i ) { }
class Foo { };

Foo(10); // construct a temporary object of type foo
foo(10); // call function foo
Foo{10}; // construct a temporary object of type foo in c++0x syntax

I think the c++0x syntax is more clear.

You could do what you want with this syntax. But beware it is very advanced and you should not do it.

MyClass.~Class(); // destruct MyClass
new( &MyClass ) Class;
caspin
The main practical reason not to do this is that if the constructor of `Class` throws, then the object `MyClass` is in an inconsistent state. Stack unwinding will attempt to destruct it again, yielding undefined behaviour. That, and it doesn't gain you anything over a well-written `operator=` or `reset` member function. So if you control `MyClass` there's no need to do this, and if you don't control `MyClass` then it's asking for trouble ;-)
Steve Jessop
+5  A: 

What happens in that line reading...

Class ();

Is that you do in fact call the constructor - for a temporary object that is being constructed from scratch, and which is then immediately destructed since you're not doing anything with it. It's very much like casting to Class, which creates a value using a constructor call, except that in this case there's no value to cast so the default constructor is used.

It's possible that the compiler then optimises this temporary away, so there's no constructor at all - I'm not sure whether that's allowed or not.

If you want to re-initialise members, calling the constructor isn't the way to do it. Move all your initialisation code into another method and call that from your constructor, and when you want to re-initialise, instead.

Steve314
"I'm not sure whether that's allowed or not" - it is, but only if neither the constructor nor destructor has any effect on the observable behaviour of the program. So if you put some tracing in to see whether they're omitted, then they can't be omitted.
Steve Jessop
+4  A: 

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.

Steve Jessop
I use constructors, but I rarely pass parameters, even though I know I can. Most of the time I just feel like I ought to set them inside the constructor in the source code. Why pass parameters to do it via main?
Matt
In that case I'm very surprised your code compiles, if you're not using constructor parameters but also not using the correct syntax for parameterless constructors. Anyway, the reason for using constructor parameters is if objects of your class aren't all the same. Take a look at the standard library for examples - you can specify the initial size and contents of a vector and so on.
Steve Jessop
Using Microsoft Visual Studio C++ and it's built-in compiler...If I declare a constructor as KnightsTour::KnightsTour (for example) and then put KnightsTour; in my headerfile, it throws errors, saying it looks like a function, but that there are no parameters. Soooo...I added an empty parameter list, i.e. "KnightsTour::KnightsTour()" and then "KnightsTour();" in the headerfile - compiles and runs!
Matt
A: 

With such requirements, I generally write a clear() (public) method. I call it from constructor, destructor. User code can call it whenever it wants to.

class Foo
{
  public:

    Foo() { clear(); }

    ~Foo() { clear(); }

    void clear(); // (re)initialize the private members

  private:

     // private members
 };

To answer the question here, the clear() method may be called whenever it is required to re-initialize the class as it was just after the initial construction.

ArunSaha