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!
C++

Avatar of undefined
Last Comment
chemicalx001

8/22/2022 - Mon
sarabande

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
sarabande

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
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
chemicalx001

ASKER
ok! I think i got it. many thanks Sara.