What does '&' mean in C++?
What does '&' mean in C++?
As within the function
void Read_wav::read_wav(const string &filename)
{
}
And what is it开发者_运维百科s equivalent in C?
If I want to transform the above C++ function into a C function, how would I do it?
In that context, the & makes the variable a reference.
Usually, when you pass an variable to a function, the variable is copied and the function works on the copy. When the function returns, your original variable is unchanged. When you pass a reference, no copy is made and changes made by the function show up even after the function returns.
C doesn't have references, but a C++ reference is functionally the same as a pointer in C. Really the only difference is that pointers have to be dereferenced when you use them:
*filename = "file.wav";
But references can be used as though they were the original variable:
filename = "file.wav";
Ostensibly, references are supposed to never be null, although it's not impossible for that to happen.
The equivalent C function would be:
void read_wav(const char* filename)
{
}
This is because C doesn't have string
. Usual practice in C is to send a pointer to an array of characters when you need a string. As in C++, if you type a string constant
read_wav("file.wav");
The type is const char*
.
It means that the variable is a reference. There is no direct equivalent in C. You can think of it as a pointer that is automatically dereferenced when used, and can never be NULL, maybe.
The typical way to represent a string in C is by a char
pointer, so the function would likely look like this:
void read_wav(struct Read_wav* instance, const char *filename)
{
}
Note: the first argument here simulates the implicit this
object reference you would have in C++, since this looks like a member method.
The ampersand is used in two different meanings in C++: obtaining an address of something (e.g. of a variable or a function) and specifying a variable or function parameter to be a reference to an entity defined somewhere else. In your example, the latter meaning is in use.
C does not have strictly speaking anything like the reference but pointers (or pointers to pointers) have been user for ages for similar things.
See e.g. http://www.cprogramming.com/tutorial/references.html, What are the differences between a pointer variable and a reference variable in C++? or http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29 for more information about references in C++.
In C, you would write it this way:
void read_wav(struct Read_wav* , const char * pSzFileName)
{
}
std::string is the C++ way of dealing with array of const char.
& is a C++ reference. It behaves just as if you were handling the pointer behind ( its is mainly a syntactic sugar, thought it prompts the contract that the pointer behind should not be null).
In this particular case, std::string has a c_str method which returns a const char *. You can make a parallel C version and then have your C++ do something like:
void Read_wav::read_wav(const string &filename)
{
do_read_wav(internal_read_wav, filename.c_str());
}
where do_read_wav is your C routine and internal_read_wav is a pointer to a C-style struct.
void do_read_wav(struct Read_wav rw, const char * filename)
Now, if you are storing information in the class, you need to make a C struct [all of the fields must be POD, etc]
References are aliases, they are very similar to pointers.
std::string
is an array of char
with an explicit length
(that is, there can be null characters embedded within).
There is a C-library to emulate std::string
(that is, provide an encapsulated interface) called bstring
for Better String Library. It relieves you from the tedium of having to deal with two distinct variables (array and length).
You cannot use classes in C
, but you can emulate them with forwarded struct (to impose encapsulation) and class methods simply become regular functions with an explicit parameter.
Altogether, this leads to the following transformation:
void Read_wav::read_wav(const string &filename);
void read_wav(struct Read_wav* this, struct bstring const* filename);
Which (apart from the struct
noise) is very similar to what you had before :)
& means that variable is passed via reference. So, inside your function you will have not a local copy of variable, but the original variable itself. All the changes to the variable inside the function will influence the original passed variable.
You might want to check out Binky Pointer fun, it's a video that illustrates what Pointers and References are.
In this case, string &filename
means the function will receive a reference (a memory address) as a parameter, instead of receiving it as a copy.
The advantage of this method is that your function will not allocate more space on the stack to save the data contained in filename.
It's a reference, as others have said. In C, you'd probably write the function as something like
void read_wav(struct Read_wav* rw, const char* filename)
{
}
&filename means this is a reference to filename.
精彩评论