Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualEddieV223

Posted 31 October 2012 - 07:37 PM

Most of the time you'll pass by reference or pointer in methods, and then copy the value to a classes member variable if you want to hold onto that value.

for example a Vector3 class
[source lang="cpp"]class Vector3{Vector3( double x, double y, double z ) : x(x), y(y), z(z) { }double x;double y;double z;};class Actor{Vector3 mPosition;void SetPosition( const Vector3 & pos ) { mPosition = pos; }};[/source]
this allows you to put in a temporary rvalue when you call the function

someActor.SetPosition( Vector3( 0.0, 0.0, 1.0) );

Which is passed by reference. The difference is that a reference is behind the scenes a pointer. A pointer being up to 64bits on 64bit os. Verses the size of the Vector3 being at least 3 * 64 ( if a double is 64 bits on your system ). This is a optimization. And since when the SetPosition class actually makes use of the reference it copies it, there is no worry about having a invalid reference.

#3EddieV223

Posted 31 October 2012 - 07:35 PM

Most of the time you'll pass by reference or pointer in methods, and then copy the value to a classes member variable if you want to hold onto that value.

for example a Vector3 class
[source lang="cpp"]class Vector3{double x;double y;double z;};class Actor{Vector3 mPosition;void SetPosition( const Vector3 & pos ) { mPosition = pos; }};[/source]
this allows you to put in a temporary rvalue when you call the function

someActor.SetPosition( Vector3( 0.0, 0.0, 1.0) );

Which is passed by reference. The difference is that a reference is behind the scenes a pointer. A pointer being up to 64bits on 64bit os. Verses the size of the Vector3 being at least 3 * 64 ( if a double is 64 bits on your system ). This is a optimization. And since when the SetPosition class actually makes use of the reference it copies it, there is no worry about having a invalid reference.

#2EddieV223

Posted 31 October 2012 - 07:35 PM

Most of the time you'll pass by reference or pointer in methods, and then copy the value to a classes member variable if you want to hold onto that value.

for example a Vector3 class

class Vector3
{
double x;
double y;
double z;
};

class Actor
{
Vector3 mPosition;
void SetPosition( const Vector3 & pos )
{
mPosition = pos;
}
};

this allows you to put in a temporary rvalue when you call the function

someActor.SetPosition( Vector3( 0.0, 0.0, 1.0) );

Which is passed by reference. The difference is that a reference is behind the scenes a pointer. A pointer being up to 64bits on 64bit os. Verses the size of the Vector3 being at least 3 * 64 ( if a double is 64 bits on your system ). This is a optimization. And since when the SetPosition class actually makes use of the reference it copies it, there is no worry about having a invalid reference.

#1EddieV223

Posted 31 October 2012 - 07:18 PM

Most of the time you'll pass by reference or pointer in functions, and then copy the value to a classes member variable if you want to hold onto that value.

PARTNERS