Click to See Complete Forum and Search --> : variables in storage classes going out of scope


peterworth
February 14th, 2005, 05:41 AM
void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
{
MyObject obj();
obj.SetMemberData(data,lengthOfData);
m_vector.push_back(obj);
}

is this code ok? or is it the case that since obj is declared in the function, when the function ends and it goes out of scope, the item at the back of the vector member in MyClass will just be garbage or null? m_vector is of type std::vector<MyObject>

Ejaz
February 14th, 2005, 05:59 AM
void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
{
MyObject obj();
obj.SetMemberData(data,lengthOfData);
m_vector.push_back(obj);
}

is this code ok? or is it the case that since obj is declared in the function, when the function ends and it goes out of scope, the item at the back of the vector member in MyClass will just be garbage or null? m_vector is of type std::vector<MyObject>

Well, the code you posted has one serious problem. You would get error C2228 with VC 6.0, saying SetMemberData must have class/struct/union type. Why? Have a look at Is there any difference between List x; and List x();? (http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2).

For your original question, the easiest way is, try to get the value out of the vector in some other function and see what you get.

peterworth
February 14th, 2005, 06:07 AM
Well, the code you posted has one serious problem. You would get error C2228 with VC 6.0, saying SetMemberData must have class/struct/union type. Why? Have a look at Is there any difference between List x; and List x();? (http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2).

For your original question, the easiest way is, try to get the value out of the vector in some other function and see what you get.

ah yes sorry, so should it be:

void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
{
MyObject obj = MyObject();
obj.SetMemberData(data,lengthOfData);
m_vector.push_back(obj);
}

?

Ejaz
February 14th, 2005, 06:09 AM
ah yes sorry, so should it be:

void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
{
MyObject obj = MyObject();
obj.SetMemberData(data,lengthOfData);
m_vector.push_back(obj);
}

?


void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
{
MyObject obj; // = MyObject();
obj.SetMemberData(data,lengthOfData);
m_vector.push_back(obj);
}



thats all :thumb:

santoct2002
February 14th, 2005, 07:13 AM
For second question.

vector:: push_back - Appends (inserts) an element to the end of a vector, allocating memory for it if necessary.

MyObject - should have proper copy constructor, assignemnt operator otherwise vector shall use default copy, assignment opertaor to make a copy.

Eventhough your local object get destroyed within a function, vector will have its own copy.

peterworth
February 14th, 2005, 08:21 AM
For second question.

vector:: push_back - Appends (inserts) an element to the end of a vector, allocating memory for it if necessary.

MyObject - should have proper copy constructor, assignemnt operator otherwise vector shall use default copy, assignment opertaor to make a copy.

Eventhough your local object get destroyed within a function, vector will have its own copy.
thats good, i just wanted to make sure the copy constructor or assignment operator is actually used even though its not explicit in the code (just calling push_back) - there is no mention in the msdn page for push_back etc wether it copies the object or not.

peterworth
February 14th, 2005, 08:24 AM
one other thing - does the same happen the other way - eg if you do something to an object you got from vector::at are you affecting the object stored in the vector or a copy of it?

RoboTact
February 14th, 2005, 08:35 AM
You can easily find yourself whether object is being copied or not. Look at the function you pass your class as a parameter to declaration. If parameter is being passed by value - copy constructor would inevitably be called (or default one if you haven't provide your own for the passing class).

Also you can add debug print code to any function of passing class to check the fact and order of calling that function in test code.

Graham
February 14th, 2005, 09:08 AM
The signature of vector<>::push_back is:

void push_back(const T& x);

It still takes a copy when it adds it to the vector, though. Passing by value invokes the copy ctor for the function call. It says nothing about the internal workings of the function.

peterworth: You can affect the stored value if you use an iterator or operator[]/at() as an lvalue. If you extract from the vector, you get a copy:

vector<int> ivec;

// fill vector...

vector<int>::iterator it = ivec.begin();
*it = 2; // Directly affects stored value
ivec[3] = 0; // Directly affects stored value
ivec.at(3) = 0; // same as above
int j = ivec[1]; // or ivec.at(1)
j = 7; // Does not affect ivec[1];

RoboTact
February 14th, 2005, 09:29 AM
Graham
You are right, it's just one-sided indication. :blush:

peterworth
February 14th, 2005, 10:20 AM
thanks everyone.