use traditional operator to make them work also for user defined types.
- '=' is created by default, but is important to specify it for class with raw pointers
- precedence and associativity can't be changed
- can't overload operator for primitive types
- can't create new operators
- [],(),->,= must be declared as memeber methods
- other operator can be either memeber method or global functions
Mystring s1 {'a'};
Mystring s2 = s1; // Not assignment, same as s2{s1} (move)
s2 = s1; // copy assigment with pointer must implement deep copy. Default is shallow copy.
Type& operator=(const Type& rhs);we assign the rhs side object to the lhf side object.
- if we don't return by reference an actual copy is returned, see the implementation by changing the return to be by value
works for rvalue reference:
Mystring s1;
s1 = Mystring{"Giosca"}; // move assignement Type& operator=(Type &&rhs);Mystring operator-() const; // unary negation
Mystring operator++(); // pre increment (++s)
Mystring operator++(); // post incremeent (s++)
Mystring operator!() const; // not operatornote object are returned by value.
Mystring operator+(const Mystring& rhs) const;- limitation: if binary operator are made as member method the lhs object must be an object of the the class
we don't have access to lhs attribute since, to overcome this usually this function are delcared as FRIEND of the class.
only one argument is provided as input
type operator-(cont type& obj) // obj is like this in member op.two argument as input
typer operator==(const type& lhs, const type& rhs);-
Doesn't make much sense to implement them as member methods
-
left operando must be a user defined class
-
not the way we do normally
```c++ Mystring larry; larry<< cout; // strange usage ```
-
we want to use them as right operand as usuale:
cout<<larry for this we implement them as functions and if needed as friend of the class.
std::ostream& operator<<(std::ostream& os, const Mystring& obj){
os<< obj._str; // if friend of the class
os<<obj.get_string(); // if not friend of the class
return os;
}we return a reference to the ostream so we can concatenate insertions
std::istream& operator>>(std::istream& is, Mystring& obj){
// get date from is and allocate as needed to obj
return is; // to chain assign
}