& appended to a class name

Jan 11, 2009 at 4:29am
I'v been looking around trying to figure out what the & at the end of a class name means. For example
D3DXVECYOR3 operator += (CONST D3DXVECYOR3);

Thanks for the help ahead of time.
Jan 11, 2009 at 5:21am
Are you meaning something like:

1
2
3
void myfunc(int &INT) {
//...
}


That means they are passing the variable by reference, which means you are actually modifying the variable that is passed.

Btw, the code you posted doesn't have a '&' in it.
Jan 11, 2009 at 4:29pm
oops here is what I meant with the code.

D3DXVECTOR3& operator += (CONST D3DXVECTOR3&);

its the postfix & rather than the prefix &.
Last edited on Jan 11, 2009 at 4:29pm
Jan 11, 2009 at 6:24pm
All the same. The ampersand (as a reference indicator) always appears after a type name. The fact that it sometimes appears before a variable name is incidental.
Your example shows a syntax that saves a little typing and maintenance.
Let's say we have a method defined as
1
2
3
4
T &T::operator+=(const T &operandB){
    this->data+=operandB.data;
    return *this;
}

We can declare this method as
T &operator+=(const T &operandB);
or
T &operator+=(const T &);
which will save us a little maintenance if we ever decide to change the parameter name, and also does a better job of hiding the implementation from the user.
But both mean the same. 'T &' means "reference to a T".

(Needless to say, the space between T and & is optional. That's just the way I like to write.)
Last edited on Jan 11, 2009 at 6:24pm
Topic archived. No new replies allowed.