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?
 
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
 
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
 
chemicalx001Author Commented:
ok! I think i got it. many thanks Sara.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.