I am learning about operator overlaoding. I have created simple class to test it.
class Beer{
public:
Beer(int oner , int twor , string name){
this -> one = oner;
this -> two = twor;
this -> name = name;
};
int getOne(){
return this -> one;
};
int getTwo(){
return this -> two;
};
string getName(){
return this -> name;
};
Beer operator + (const Beer &a)const {
return Beer(5,two+a.two,"firstName");
};
Beer operator + (string a)const {
this -> name = this -> name +" "+a;
};
private:
int one;
int two;
string name;
};
I am trying to figure out , how to midify the string with overloaded operand. My function i declared
Beer operator + (string a)const {
this -> name = this -> name +" "+a;
};
Throws error about passing const string.
I tried using
Beer operator + ( const string *a)const {
swap(this -> name , this -> name + " " + a);
return *this;
};
Which complained about one being cosnst string , and secon one being basic string.
The idea is simple.
Beer one ( 5, 6, "one")
one + "two"
// one.name = "one two"
What is the right way how to do it?
// error with swap
error: no matching function for call to 'swap(const string&, std::basic_string<char>)'|
// erro with string
passing 'const string {aka const std::basic_string<char>}' as 'this' argument of 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(std::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' discards qualifiers [-fpermissive]|