I am in the process of learning C++ in order to understand some open source code I have been given.
I came across a line as follows:
cmd << '\n'
I assumed that "cmd" must be some kind of special receptor for a stream, perhaps a string - but on further investigation I found that "cmd" was an entire class with assorted data and functions. This has completely confused me. Why doesn't the code look like this:
cmd.stringpart << '\n'
开发者_运维百科Can someone tell me what's going on, or suggest an article for me to take a look at.
CORRECTION: cmd is an instance of a class rather than the class itself.
In C++, you can overload operators. In this case it seems to be used to make some Cmd class behave like a stream.
See operators as functions: For instance, 3 + 4
calls a binary function taking two numbers and returning the sum of them.
Here, the author has created such a function to define the << operator, so that it can work with a cmd class instance as the left parameter, and a string as the right parameter. This is called "operator overloading". Look for operator<< occurrences in your code.
This can also be a member function of the cmd class, taking one parameter (still named operator<<).
I'd first check to see if the class of which cmd
is an instance overrides the <<
operator - that would show you what is going on in this code.
check for operator overloading in this class - you should seek for function with '<<' in it's name.
It sounds like the ostream operator (<<) has been overloaded. Look for a method called "operator<<" in the class definition. C++ allows programers to "overload" or redefine the way operators (including +,-,*,/,++,--, etc) work with their classes. Consult any C++ text for a discussion of this.
In many languages including C++ you are allowed to do what it is called "operator overload"
In your code if you have an operation for example that you perform repeatedly, generally for the sake of cleaner and handier code, you can pick an existing operator (such as --, +=, ==, <<, >>) and re-define it locally aka overriding, then use it as you please.
There are also some cases that you override the operator to create the functionality you want in a library. For example: - You created a class mainly to store blocks of information. Then, you use container classes (from various libraries) to store instances of your class.
Foo {
int a;
int b;
std::string c;
}
So usually every container class will have methods to juggle around, delete, add, compare (...) instances stored. Let's say you want to compare an instance of Foo foo with woo = FooContainer[0] stored in your container with "==" in, some libraries will require you to re-define so override the "== " operator anyways, where some will assume. For instance, they will return true only if( foo.a == woo.a && foo.b == woo.b && foo.c == woo.c ) is true.
Well, maybe two instances is the same for your use only if their 'c' is the same, then you can just override "==". You typically would do that in Foo, right under the constructor. It'd look similar to this:
bool operator==(const Foo &foo) const {return c == foo.c;}
Bottomline:
- If there is an operation you couldn't make sense, you might want to look at technical guide if exist. If not, go to header files, override classes etc to explore overwritten operators before go deep in the code.
- You also wanna get familiar with them yourself, because (a) you might HAVE to use in some cases, (b) it might give you functionality you want super easily, (c) it can make your code clean and simple.
Hope it helps..
精彩评论