vector<class> problem

Hello,

I'm having a problem with storing classes in a vector. I'm for now just using a simple windows program to which I added this struct:
1
2
3
4
5
6
7
8
struct STEST {
  STEST(){
    MessageBoxA(NULL,"make","make",NULL);        
  }     
  ~STEST() {
    MessageBoxA(NULL,"destruct","destruct",NULL);
  }     
};

I also added the global variable:
 
vector<STEST> vTest;

Now before the message loop I added this code:
 
vTest.push_back(STEST());

Now when I run the program it instantly displays:

- make
- destruct

and when I close the program once again:

- destruct

It is somehow destructing my class two times. The first time I really don't want it to do this. Why is this happening and how do I make sure it doesn't happen?
It looks like you're structure is just doing what it is told and calling the two functions right away. I'm curious what is the reason for the structure here? Windows has ways to handle this operation by default why are you creating your own?

EDIT: For those of you who didn't catch on MessageBoxA(...) is a common windows API function so this is a safe assumption.
Last edited on
Thank you for your reply.

This is just a test for a bigger program I'm trying to create. My problem is that the class is instantly destroyed after it is created and I don't know why this is happening.

As I said (might be unclear):

Before the GetMessage loop, The program gives me 2 messages
-make
-destruct
This indicates that my struct is indeed being created, but it is also right after it being destroyed.
At the end of the program (when I click the X) the destruct message once again comes in...

If I push 2 STEST() like this:
1
2
vTest.push_back(STEST());
vTest.push_back(STEST());

it would show:

-make
-destruct
-make
-destruct

and when I click the X:
-destruct
-destruct

I'm still puzzled :)
Try declaring STEST as a class instead of a struct. Assign data types to your functions. The way you presented this is a strange way to use the struct\class capabilities of C++ so I'm sorry if I seem slow to catch on.

What type is vTest? Can I assume that they are STEST structures that you declared? push_back() doesn't seem to be a function in your prototype. I'm confused :,(.
Last edited on
The problem is that you are calling a copy constructor. And you are not showing a message for that because you have not overridden the one the compiler provides for you.
In the original post:
1
2
vTest.push_back(STEST());//creates a temporary STEST object (make), which is copied into
//the vector, then the temporary object is destroyed(destruct). 


The object being destructed at the close of the program is the STEST object that was stored in the vector (destruct)
Isee, that's odd =) I'll get used to that I suppose.

Thanks alot for your replies.

And thanks computergeek for trying ;)
@guestgulkan

exactly , when you create a vector contains class(or structs), it actually copys the newly-constructed object and stored in it . Please see the quotes from Effective STL(Item 3)

Instead, when you add an object to a container (via. e.g.. insert or push_back. etc.),
what goes into the container is a copy of the object you specify. When you get an
object from a container (via. e.g.. front or back), what you set is a copy of what was
contained. Copy in, copy out. That's the STL way.


Hope this helps you
Topic archived. No new replies allowed.