Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


destructor and vector question

Posted on 2004-11-10
Medium Priority
Last Modified: 2012-06-27
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 !
Question by:themuppeteer
  • 2
LVL 12

Expert Comment

ID: 12542735
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.


LVL 48

Accepted Solution

AlexFM earned 400 total points
ID: 12542760

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.

Author Comment

ID: 12542993
AlexFM you are 100% right. I did not know the vector did that.

thanks a lot!

Author Comment

ID: 12543007
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...


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question