Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 694127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:49:30+00:00 2026-05-14T02:49:30+00:00

I wanted to run 1,000 iterations of a program, so set a counter for

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:49:30+00:00Added an answer on May 14, 2026 at 2:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to add <br> after every 2 loops. The loop will run 8
In a macro for Visual Studio 6, I wanted to run an external program,
I was finishing up a code mod and wanted to run my program through
I wanted to run my application on iPhone simulator via some script. Is it
I just asked a question earlier today because I wanted to run an executable
With cron, if I wanted to run a command every 5 mins it would
I've run into a little hurdle and wanted to see if somebody could help
I wanted to run some Ant scripts from a build.xml in my PHP project.
I wanted to run a .cmd file from Java. I have something which works
I just wanted to run my login script by you guys to see if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.