My code is just like below:
class CDemo {
public:
CDemo()
: str(NULL){
};
~CDemo() {
if (str)
delete[] str;
};
char* str;
};
void testVector() {
CDemo c;
c.str = new char[32];
strcpy(c.str, "abcdef");
vector<CDemo>* v = new vector<CDemo>();
v->push_back(c);
delete (v);
return;
}
I know this is a code which will report a run-time error because I didn't explicitly define a copy constructor, so ,when testVector
returned , the member field str
will be freed twice, thus reporting an error.But what make me confused , is that when I debug to the line: delete(v)
, the visual studio shortcut is as below:
I think the function push_back
will lead to the call of the default copy constructor
of object c
, so, after push_back
returned , the element of vector v
should be a different object from c
, but from the debug watch, I find they are the same address:0x00318100
. Does it prove that they are actually the same object?
Anyone can explain this?