overloading operator mysting example. operator overloading 1+2 matrix m 1 + m 2 using traditional...
TRANSCRIPT
Overloading Operator
MySting Example
Operator Overloading
• 1+2• Matrix M1 + M2
• Using traditional operators with user-defined objects
• More convenient• but may make program difficult to
understand
Operator Overloading
• Function can be overloaded• Operators can also be overloaded as ordinary functions,
– overload built-in C++ operators: give different meaning to operators such as +, +=, <<, etc.
– taking one or more arguments of class or reference– Keyword operator
• To use an operator on a class object it must be overloaded except the assignment operator(=)or the address operator(&)– Assignment operator by default performs member_wise
assignment – Address operator (&) by default returns the address of an object
Overloading an operator
• Write function definition as normal• Function name is keyword operator followed by the
symbol for the operator being overloaded• operator+ used to overload the addition operator (+)• add function• + operator
Prototype
class foo{public :
foo operator--(); // overload unary minusfoo operator-(int); //binary minus foo-intfoo operator-(foo); // foo - foo
};foo operator-(int, foo) ; // int - foofoo operator-(int, foo*) ; // illegal need foo or foo&
Member vs non-member functions
• Operator functions can be member or non-member functions– When overloading ( ), [ ], -> or any of the
assignment operators, must use a member function
• Operator functions as member functions– implicitly acting on this object– Leftmost operand must be an object (or reference to an object)
of the class– If left operand of a different type, operator function must be a
non-member function
Non-member Function
• Operator functions as non-member functions– Must be friends if needs to access private or
protected members– Enable the operator to be commutative
Overloading restrictions
• Can not create new operators• The precedence, associativity and arity do not
change • can not overload operators for built-in types– Cannot change how two integers are added
MyString
• Design MyString class:– Represent a sequence of characters – the length of the string is important– data member:
• s : pointer• len
– methods: • constructor: int n or char *• length, • print, • destructor
Declare the Class my_string
class MyString{public:
MyString(int n); MyString(const char * str);~ MyString();int length() const ; //const member functionvoid print() const;
private:char * s;int len;
};
Constructors
MyString :: MyString(int n){
s = new char[n+1];s[0] = ‘\0'; len =n;
}
MyString :: MyString(const char * str){ len = strlen(str); s = new char[len+1];
strcpy(s,str); }
Overload Operator + (MyString) member function
Prototype:MyString operator +(const MyString &b) const;Definition:MyString MyString ::operator+ (const MyString &b) const{ cout << "member +" << endl; char * temp = new char[this->len + b.len +1]; strcpy(temp, this->s); strcat(temp, b.s); MyString str(temp); return str;}
Returning Local Object from a function
• Returning an object invokes the copy constructor while returning a reference doesn't.
• If a method or function returns a local object, it should return an object, not a reference.
• It is an error to return a pointer to a local object. Once the function completes, the local object are freed. The pointer would be a dangling pointer that refers to a nonexistent object.
Overload Operator + non-member function
friend MyString operator+(const MyString & a, const MyString &b);
MyString operator+(const MyString &a, const MyString &b) { cout<< "nonmember +" << endl;
char * temp = new char[a.len + b.len + 1]; strcpy(temp, a.s); strcat(temp, b.s); MyString ss(temp); return ss; }
Overloading the Assignment Operator
copy constructorscopy constructors Called when a new object is created and set equal to an Called when a new object is created and set equal to an existing instanceexisting instance
What’s the difference between the following lines of What’s the difference between the following lines of code?code?MyString str1,str2;MyString str1,str2;
……
// What’s the difference between the following two// What’s the difference between the following two
// lines of code?// lines of code?
MyString aString = str1;MyString aString = str1;
str2 = str1;str2 = str1;
The first assignment involves a copy constructor The first assignment involves a copy constructor since a new object is being created.since a new object is being created. The second is straight assignment to an existing The second is straight assignment to an existing variable, so no copy constructor is involved.variable, so no copy constructor is involved.
Overloading the Assignment Operator (cont)
Remember, C++ will define a default and naïve copy constructor for you Remember, C++ will define a default and naïve copy constructor for you if you don’t provide one. if you don’t provide one. It will just copy member variables (potential for dangling pointers)It will just copy member variables (potential for dangling pointers) In the case of MyString, we’d need to override the default copy In the case of MyString, we’d need to override the default copy constructor to make sure the storage was copied properly.constructor to make sure the storage was copied properly.
MyString::MyString(MyString &aCopy)MyString::MyString(MyString &aCopy)
{{ // Copy storage into new instance if necessary... // Copy storage into new instance if necessary...}}
Again, this will take care of the case where someone tries to assign to Again, this will take care of the case where someone tries to assign to
a MyString variable when it is declareda MyString variable when it is declared:: MyString aStr = anotherStr;MyString aStr = anotherStr;
Overloading the Assignment Operator (cont) However, when we need to handle the case where an existing variable However, when we need to handle the case where an existing variable is assigned a new value via the assignment operator, we is assigned a new value via the assignment operator, we overloadoverload the the assignment operator:assignment operator:
The The == operator is another special case binary operator operator is another special case binary operator......
MyString &MyString::operator=(const MyString &sourceStr)MyString &MyString::operator=(const MyString &sourceStr)
{{ //set the value //set the value
return *this; // Huh?return *this; // Huh?
}}
Remember that when overloading the = operator you are going to be Remember that when overloading the = operator you are going to be assigning to an existing instance. If that instance has dynamically assigning to an existing instance. If that instance has dynamically allocated data it should be freed.allocated data it should be freed. We return a reference so that str1 = str2 = str3 works...We return a reference so that str1 = str2 = str3 works...
Overloading the Assignment Operator (cont) we should always make sure that our source and we should always make sure that our source and destinations aren’t the same..destinations aren’t the same.. We do this by adding the following code:We do this by adding the following code:
MyString & MyString::operator=(const MyString &sourceStr)MyString & MyString::operator=(const MyString &sourceStr)
{{ // Make sure we actually have two pointers // Make sure we actually have two pointers
if (this != &sourceStr)if (this != &sourceStr)
//set the value//set the value
return *this; return *this;
}}
“ “thisthis”, ”, is a pointer to the current instance of the class we is a pointer to the current instance of the class we are in.are in.
Overload = (MyString)
MyString & MyString ::operator =(const MyString & str){
if (this != &str) //important, avoid self-assignment { len = str.len; delete [] s; s = new char [len]; strcpy(s, str.s); } return *this;}
Overload += (MyString)
// += operatorMyString &MyString ::operator+=(const MyString &str){
this->len += str.len + len ;char *tempPtr = new char[len +1];
strcpy(tempPtr, s); strcat(tempPtr, str.s);
delete []s; s= tempPtr; return *this;}
Overload Subscripting
char & my_string::operator [](int i){
return this->s[i];}
my_string ms1(“hello”);ms1[0] = '!'; // subscript operator [], returning reference
Friend Functions
• The keyword friend is function specifier• It gives nonmember function access to the
hidden members of the class• escaping the data hiding restriction• Make sure you have a good reason for
escaping the restriction
Declare friend functions
• Declare in the class to which the function is a friend• Good: put in the public part of the class• Member functions of one class can be friend of
another class, use the class scope quantifier• All member functions of one class are friend
functions of another, just write friend class classname
Overloading I/O Operator <<
• The operator << has two arguments– ostream &– the ADT
• It must produce an ostream &• Use a reference since you don’t want to copy
the stream object
Overload << (my_string)
• friend ostream & operator<<(ostream &, const my_string &);
ostream& operator<<(ostream &out, const my_string &str)
{return (out << str.s << endl);
}
>>
istream& operator >> (istream &out, my_string &str)