lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

Operator overloading.md (485B)


      1 +++
      2 title = 'Operator overloading'
      3 +++
      4 # Operator overloading
      5 an operator is just a function with different syntax
      6 you cannot define any new operators
      7 
      8 ex:
      9 
     10 ```c++
     11 enum class Month{
     12 	jan=1, feb, mar, apr, may, jun, jul, aug, sept, oct, nov, dec
     13 };
     14 
     15 vector<string> month_table {“January”, “February”, ..etc};
     16 
     17 ostream& operator<<(ostream& os, Month m) {
     18 	return os << month_table[int(m)-1];
     19 }
     20 
     21 int main() {
     22 	Month m {Month::dec};
     23 	cout << m;
     24 
     25 	// returns “December”
     26 }
     27 ```