• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ : Passing vector to method

Carlis

Senior member
I want to pass a vector to a method where values are set. Something like this

void myClass::func1()
{
vector<double> density;
density.assign(2,0);
func2(density);

(use density)
}

void myClass::func2(vector<double> density)
{
density[0]=this;
density[1]=that;
}

------

Now; when plotting the values of density in func2, it has the correct values, but in func1 it is just zero. Is there any way of sending the vector into a function in the same way as one would do with an ordinary array?

Best
Carlis
 
You need to pass a pointer or pass by reference.

ex:

void myClass::func2(vector<double>* density) or
void myClass::func2(vector<double>& density)

using a pointer requires you to dereference for access... (*density)[0] = that

EDIT: also when passing with a pointer you need to call with: func2(&density); to get the address of the vector.
 
Last edited:
What Obscure said. Vectors behave more like standard variables then arrays do in c++ You wouldn't expect

Code:
void foo(int bar)
{
   bar = 7;
}

void main()
{
   int oldBar = 8;
   foo(oldBar);
  cout << oldBar << endl;
}

You wouldn't expect the above to spit out 7. So why would you expect a change to be made to anything else passed into a different function?

Pass by reference is for Speedy function calls and to send in data you want to smash. You can pass in pointers if you like, but I prefer to just use reference for everything as it is essentially the same thing, just less writing when calling the function.
 
snip

Pass by reference is for Speedy function calls and to send in data you want to smash. You can pass in pointers if you like, but I prefer to just use reference for everything as it is essentially the same thing, just less writing when calling the function.

What do you mean by 'speedy function calls'?

Like passing by reference in C++ is faster than passing a pointer? Or just that passing a vector by value will result in copying the whole object?
 
What do you mean by 'speedy function calls'?

Like passing by reference in C++ is faster than passing a pointer? Or just that passing a vector by value will result in copying the whole object?

Passing by reference is (usually) faster then passing by value. There are cases where it isn't (Simple data types, such as ints). However, anything that is more complex then an int will be passed faster by reference.

The speed of passing by reference and passing by pointer is the same. Passing by value slows things down because each value has to be copied onto the stack. That can take quite a few instructions for a big object.
 
Back
Top