简体   繁体   中英

getting pointer to a vector C++

this is my second basic question on pointers. I am calling a function exposed in DLL..

A vector is being declared and populated with values inside that function being called.

I need to loop through the vector and access its values from the calling function.

int calling_function()
{
int* vectorSize;
string input = "someValue";
vector<customObjects> *v;// do i need a pointer to a vector here?

void function_being_called(input,v,&vectorSize);

//need to access the vector here...

}

void function_being_called(string input, void *returnValue, int* vectorSize)
{
vector<customObjects> v;
v.push_back(myObj);

*vectorSize= v.size();

*returnValue = ? // how to pass vector to the calling function through this parameter pointer variable

return;
}

It should be like this:

int calling_function()
{
  string input = "someValue";
  vector<customObjects> v;

  function_being_called(input,&v);

  // access the vector here...

}

void function_being_called(string input, vector<customObjects>* v)
{
  v->push_back(myObj);
}

You've got two options. First, pass the vector as a reference:

string input = "someValue";
vector<customObjects> v;
function_being_called(input, v);

void function_being_called(string input, vector<customObjects> &v)
{
 // Whatever
}

Or, if you're using C++11 just return a vector and let the move constructor take care of it:

string input = "someValue";
vector<customObjects> v =  function_being_called(input);

vector<customObjects> function_being_called(string input)
{
  vector<customObjects> v;

 // Whatever

  return v;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM