variables in storage classes going out of scope
Code:
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>
Re: variables in storage classes going out of scope
Quote:
Originally Posted by peterworth
Code:
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();?.
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.
Re: variables in storage classes going out of scope
Quote:
Originally Posted by Ejaz
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();?.
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:
Code:
void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
{
MyObject obj = MyObject();
obj.SetMemberData(data,lengthOfData);
m_vector.push_back(obj);
}
?
Re: variables in storage classes going out of scope
Quote:
Originally Posted by peterworth
ah yes sorry, so should it be:
Code:
void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
{
MyObject obj = MyObject();
obj.SetMemberData(data,lengthOfData);
m_vector.push_back(obj);
}
?
Code:
void MyClass::AttachObj(unsigned long* data, iny lengthOfData)
{
MyObject obj; // = MyObject();
obj.SetMemberData(data,lengthOfData);
m_vector.push_back(obj);
}
thats all :thumb:
Re: variables in storage classes going out of scope
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.
Re: variables in storage classes going out of scope
Quote:
Originally Posted by santoct2002
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.
Re: variables in storage classes going out of scope
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?
Re: variables in storage classes going out of scope
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.
Re: variables in storage classes going out of scope
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:
Code:
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];
Re: variables in storage classes going out of scope
Graham
You are right, it's just one-sided indication. :blush:
Re: variables in storage classes going out of scope