M
Mike Darrett
Hello,
First off, I'm a C++ newbie, so please turn your flame guns off.
I'm wondering if this is the correct way to have a function return a
vector:
vector<int> GetVecData()
{
vector<int> ret;
ret.push_back(2); // enter data
ret.push_back(5);
ret.push_back(8);
ret.push_back(3);
return ret;
}
Can anyone tell me what exactly is happening in the above code? (Are
the vector data points copied? Or is something else happening?)
I'm guessing (hoping) it isn't doing something like the following:
int *GetArrayData()
{
int data[10];
data[0] = 2; // enter data
data[1] = 5;
data[2] = 8;
data[3] = 3;
return &data[0];
}
The above code should fail, since the data array is destroyed once the
function finishes. (Right?)
Thanks!
Mike Darrett
First off, I'm a C++ newbie, so please turn your flame guns off.
I'm wondering if this is the correct way to have a function return a
vector:
vector<int> GetVecData()
{
vector<int> ret;
ret.push_back(2); // enter data
ret.push_back(5);
ret.push_back(8);
ret.push_back(3);
return ret;
}
Can anyone tell me what exactly is happening in the above code? (Are
the vector data points copied? Or is something else happening?)
I'm guessing (hoping) it isn't doing something like the following:
int *GetArrayData()
{
int data[10];
data[0] = 2; // enter data
data[1] = 5;
data[2] = 8;
data[3] = 3;
return &data[0];
}
The above code should fail, since the data array is destroyed once the
function finishes. (Right?)
Thanks!
Mike Darrett