destructor and vector question

I'm confused.
I've written a very simple class (called Region) that puts a line on the screen in the constructor and one in the destructor. I have a static int in that class keeping the number of instances (incremented by one in every constructor) and a private int that keeps the id of every instance (so in the constructor I have mynumber = numberofinstances).
This being said, this is the code (it is testcode, I was checking how vector does its destruction):

void testMethod()

       Region region;      
            vector<Region> regv;
            Region r2;
            printf("after creation vector");
            printf("after first pushback");
            printf("after second pushback");
      printf("after everything");


On screen I get:

Constructor Region: instances:0 // creating my 2 instances
Constructor Region: instances:1
after creation vector
after first pushback
destructor Region class for number 0 //  ?? why here
after second pushback
destructor Region class for number 1 // destructor vector
destructor Region class for number 0
destructor Region class for number 1 // out of scope second object due to }
after everything
destructor Region class for number 0 // out of scope first object due to end of method

I don't understand this. I do understand that my objects get destroyed twice, once for getting out of scope and once by the destructor of the vector.
But I don't understand the parts with the "??" next to them.

Can anyone explain this ?

Thanks !
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi themuppeteer,
It looks like you have inconsistent output order. If you want to mix C-style IO (like printf) with C++ IO, make sure that none is buffered.



This line creates new instance of Region using copy constructor and keeps it inside of vector. Add copy constructor to the Region class with appropriate output and you will see what happens.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
themuppeteerAuthor Commented:
AlexFM you are 100% right. I did not know the vector did that.

thanks a lot!
themuppeteerAuthor Commented:
thanks for replying to stefan, well ,actually it wasn't buffered but I'm switching between a linux machine and a windows pc and I type it over. I forgot the '\n' at the end of my printf...

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.