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!
chemicalx001Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
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
0
sarabandeCommented:
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
0
chemicalx001Author Commented:
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.
0
sarabandeCommented:
what is the Tar array?

if you want to return an array you might consider to return it as return value like I did in my second sample. that way the caller doesn't need to provide a second (empty) array by reference (or pointer) but simply could do

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;
}

int main()
{
     ...
      pointf points[] = { { 1.1, 2.2 }, { 0.9, 3.1}, { 5.5, 0.2 }, };
      int npts = (int)(sizeof(points)/sizeof(points[0]));
      std::vector<pointf> inparr(&points[0], &points[npts]);
      std::vector<pointf> newarr = dothings(inparr);   // get the changed array back
      ...
}

Open in new window


the advantage is that the caller is free to decide whether they need two arrays or simply assign the changed array to the input array.

inparr = dothings(inparr);  // here we change the input array

Open in new window


Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chemicalx001Author Commented:
ok! I think i got it. many thanks Sara.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.