destructor and vector question

Posted on 2004-11-10
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
    LVL 12

    Expert Comment

    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


    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.
    LVL 2

    Author Comment

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

    thanks a lot!
    LVL 2

    Author Comment

    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...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now