开发者

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

开发者 https://www.devze.com 2022-12-25 01:28 出处:网络
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 initializa

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 me开发者_JS百科an - 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?


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.


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.


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


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号