Puzzling C++ syntax using "&" [duplicate]
Possible Duplicate:
What is the meaning of & in c++?
Here is a C++ class declaration snippet containing an ampersand that doesn't 开发者_Python百科make sense to me:
class rimage : public matrix {
public:
rimage( void );
rimage( int, int );
rimage( rimage & );
rimage & operator=( const matrix & A );
rimage & operator=( const rimage & A );
rimage & operator/=( int );
...
What does rimage( rimage & );
mean? Why is the &
at the end?
This snippet was from a text book on PCNNs
This class declaration is giving me compilation problems so I need to figure this out.
The declaration
rimage ( rimage & )
Might be easier to parse if you give the argument a name:
rimage ( rimage & A )
From this, it's a bit clearer that this defines a copy constructor that takes in another rimage
by non-const
reference.
More generally, when you leave the names of parameters out of function declarations, they can sometimes be a bit harder to read. For example, this function
void DoSomething (int (int))
Can be hard to read, but if you add in a parameter name like this:
void DoSomething (int function(int))
Now you can see a bit more clearly that it's a function that takes another function in as a parameter.
For something even harder, consider
void DoSomething (int (&)[5])
With a parameter it's a bit easier to see what this is:
void DoSomething (int (&array)[5])
This is a function that takes in a reference to an array of five integers.
Hope this helps!
The &
signifies that it is an argument being passed by reference. In this case, the rimage(rimage&)
is the declaration for a copy constructor.
It's a reference to an 'rimage' object
https://isocpp.org/wiki/faq/references
It's probably just confusing you because it's unnamed in this declaration.
精彩评论