friend ostream &operator<<(ostream &os, const CSnmpMaintenanceSwitch &sw);

- 68,383
- 11
- 101
- 131

- 37
- 1
-
2It overloads `operator <<` for the class `CSnmpMaintenanceSwitch`. – Alok Save Jun 24 '13 at 06:45
-
What is mysterious or unclear in this declaration? – curiousguy Jun 28 '13 at 15:10
4 Answers
Well, it means it exists an operator << (certainly in global namespace or in a namespace of your current class) taking an ostream & as a first parameter and a const CSnmpMaintenanceSwitch & as a second parameter and this operator may access private members of your current class (which is certainly CSnmpMaintenanceSwitch)

- 3,145
- 2
- 20
- 26
Literally? It's a declaration for a freestanding <<
operator that has an ostream
on the left and a CSnmpMaintenanceSwitch
on the right. The expression yields an ostream
reference.
Semantically? It's a stream insertion operator. You know how you can say std::cout << someString;
? Well, that's because somewhere, there's a declaration that says ostream& operator<<(ostream& os, const string &str);
. The line you're asking about was almost certainly found in the CSnmpMaintenanceSwitch
class, and gives that operator the access it needs to output one of those objects. So now you can say std::cout << mySnmpMaintenanceSwitch;
as well.

- 84,970
- 20
- 145
- 172
This line declares an operator<<
which takes two parameters: a reference to an instance std::ostream
and a constant reference to an instance of CSnmpMaintenanceSwitch
. It will allow you to write some information about an instance of CSnmpMaintenanceSwitch
to output stream like
operator<< ( std::cout , CSnmpMaintenanceSwitchInstance );
or
std::cout << CSnmpMaintenanceSwitchInstance;
Operator is declared as friend
so it could access private and protected members of CSnmpMaintenanceSwitch
.
The first parameter is declared as non-constant reference since the output stream should be modified(some data will be written to it) by the default nature of oeprator<<
.
The second parameter is declared as a constant reference because operator<<
should not change it (by it's default nature).

- 5,973
- 7
- 46
- 81
the reason why it is deaclared as friend
is that you may want to directly reach and use some private variables in the overloaded operator.
Because you give the object CSnmpMaintenanceSwitch
as a constant reference
, it is quaranteed that it will not modify the private data

- 7,789
- 1
- 26
- 48