c++ - Understanding the Syntax of Overloaded Insertion / Extraction Operators (& Operator Placement) -


its been few months since last programming class (back in spring) , i'm trying practice overloading operators of class before go class. 1 thing never quite understood placement of & operator in function name. example:

ostream & operator << (ostream & out, const measurement & x) 

the placement of first & seems bit odd because place reference operator in front of object want become reference, first 1 placed before operator <<. operator << object here?

i apologize, our professor never explained syntax, taught not question it, don't prefer. i'd understand syntax entirely.

edit: i've included full function below.

ostream & operator << (ostream & out, const measurement & x) {     out << "inches : " << x.inches;     out << "\nfeet   : " << x.feet;     out << "\nmiles  : " << x.miles;     out << "\n";      return out; } 

you confused because symbol & means different in different context:

int i, j; int* ptr = &i;  // & used variable, , operator "address-of" int r = & j;  // & used 2 variables , operator "bitwise-and" int& ref = i;   // & used type, not variable , not operator anymore,                  // type modifier, says type not `int`                 // reference `int` 

so in code:

ostream& operator << ( ostream& out, const measurement& x); 

& used types ostream , measurement , modifies types references


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -