|
-
February 14th, 2005, 06:41 AM
#1
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>
-
February 14th, 2005, 06:59 AM
#2
Re: variables in storage classes going out of scope
 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.
Last edited by Ejaz; February 14th, 2005 at 07:03 AM.
-
February 14th, 2005, 07:07 AM
#3
Re: variables in storage classes going out of scope
 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);
}
?
-
February 14th, 2005, 07:09 AM
#4
Re: variables in storage classes going out of scope
 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
-
February 14th, 2005, 08:13 AM
#5
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.
-
February 14th, 2005, 09:21 AM
#6
Re: variables in storage classes going out of scope
 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.
-
February 14th, 2005, 09:24 AM
#7
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?
-
February 14th, 2005, 09:35 AM
#8
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.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
February 14th, 2005, 10:08 AM
#9
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];
Last edited by Graham; February 14th, 2005 at 10:11 AM.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 14th, 2005, 10:29 AM
#10
Re: variables in storage classes going out of scope
Graham
You are right, it's just one-sided indication.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
February 14th, 2005, 11:20 AM
#11
Re: variables in storage classes going out of scope
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|