Some dodgy C++ knowledge here.
Code:
void foo(int *bar)
{
&bar=45;
}
Would be:
Code:
void foo(int *bar)
{
*bar=45;
}
Why you have a & (the symbol for a reference) I am not entirely sure. You might call the function as such:
Code:
foo ( &mrBar );
And besides which, returning the int is entirely possible and much more understandable. As for the char* doc example, I would use the std::string class so that's also a bad example in any case. An example of where you might need pointers because they're inescapable is a function like this:
Code:
int x = 1;
int y = 2;
swapTwoVariables ( &x, &y );
printf ( "%i and %i", x, y );
Which would return "2 and 1" (noting the swap). Functions like that tend to be a bit crappy and I would say that if you're swimming in pointers that don't make apparent sense then you're doing something wrong. A 'real world' example of pointers is something like the DirectX API are absolutely chock full of pointers and references:
Code:
D3DXMatrixRotationY ( &upMatrix, YDelta / 400 );
D3DXVec3TransformCoord ( &this->upVector, &this->upVector, &upMatrix );
Note all the use of &, which is "address" (creating a pointer to the variable instead of a copy). So it definitely has got practical applications.
What you meant by "public variable" makes no sense. It's a function, not a class, in Pyro's example.
All this said, I don't like pointers and references very much in C++ either and try to avoid them where possible. This is not to say they don't have their uses, and they're not overly complex to understand, but by and large you can avoid them.
Bookmarks