tags:

views:

191

answers:

5

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.

+4  A: 

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.

Jackson Pope
+2  A: 

check for operator overloading in this class - you should seek for function with '<<' in it's name.

foret
+2  A: 

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.

Echelon
+6  A: 

In C++, you can overload operators. In this case it seems to be used to make some Cmd class behave like a stream.

Sebastian N.
+3  A: 

See an operator as a function: For example, 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<<).

Samuel_xL