Link to home
Start Free TrialLog in
Avatar of chemicalx001
chemicalx001

asked on

c++ pointers...

Quick c++ question, possibly an idiotic one...

If i have a function, say:

 vector<Point2f> dothings(vector<Point2f> cornerPoints)
 {

int 2 = 2;
int 3 = 3;

cornerPoints.push_back(2);
cornerPoints.push_back(3);

}


and i want to call it like this:


dothings(pointArray);


BUT, i to also specify the resulting array. So I pass it 'pointArray', but I want it to give back a NEW array, with the additions from the function.

So i want to say:

dothings(pointArray, newArray);

And newArray will be pointArray, plus the result of the dothings function.

How do i do this? a return? or a pointer?
thanks!
Avatar of sarabande
sarabande
Flag of Luxembourg image

the simplest is to pass the vector by reference. than you can add (or remove) entries and the caller will get the changed array back.

bool dothings(std::vector<pointf> & arr)
{
     arr.erase(arr.begin());
     arr.push_back(pointf(1.1, 2.2));
     return !arr,empty();
}

Open in new window


alternatively you would pass the input array as const std::vector<pointf> & and return a new array by value.

std::vector<pointf> dothings(const std::vector<pointf> & inparr)
{
     std::vector<pointf> arr = inparr;
     arr.erase(arr.begin());
     arr.push_back(pointf(1.1, 2.2));
     return arr;
}

Open in new window


you would do the second if the arrays are small (say less than 100 points) and if the input array might be preserved by the caller.

Sara
vector<Point2f> dothings(vector<Point2f> cornerPoints) {
 int 2 = 2;
 int 3 = 3

variables in c or c++ may not begin with a digit. the compiler would not be able to distinguish between constants and variables.

vector<Point2f> dothings(vector<Point2f> cornerPoints)  {
 int x = 2;    
 int y1 = 3;

Open in new window

 

Sara
Avatar of chemicalx001
chemicalx001

ASKER

thanks Sara, forgive my idiocy, I need to return a different vector than the one that is passed.
Something like:

std::vector<pointf> dothings(const std::vector<pointf> & inparr, vectorThatGetsReturned)
{
     std::vector<pointf> arr = inparr;

    Tar.push_back(arr[0]);
     return Tar;
}

But i need to specify when i pass the vector what BOTH vectors will be.

dothings(array, newArray);

so that I can call the 'Tar' array.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok! I think i got it. many thanks Sara.