lect11_2

Lect11_2 - MyString Class Revisited 1 Additional Class Features Part 2 MyString Class Revisited In this part we expand the MyString class adding

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: MyString Class Revisited 1 Additional Class Features - Part 2 MyString Class Revisited In this part we expand the MyString class; adding more advanced features. Returning references In C++, a function may return references. Returning references can be very useful. A function that returns a reference can be used on the left side of an assignment statement. The effect of this is very powerful; we demonstrate this soon with the operator function. We begin with a simple program having a function that returns a reference. #include <iostream> using namespace std; int intVal; // function retRef declaration int& retRef(); void main() { retRef() = 55; cout << intVal; // outputs 55 } // end main function retRef definition int& retRef() { return intVal; } // end retRef We declared the function retRef() to return a reference to int . In the function definition, the statement return intVal; // returns a reference to intVal will not return the value of the global variable intVal , but returns the variable intVal itself. In the main function, the statement retRef() = 55; // gets translated as intVal = 55; MyString Class Revisited 2 sets the value of intVal to 55 , because retRef() returned a reference to intVal . We must be careful when returning a reference. The object that we refer to must not go out of scope. For example, consider the following rewritten version of retRef() . int& retRef() { int x = 55; // x now is a local variable return x; // returns a reference to x } // end retRef In the above function, x is local to retRef() and will go out of scope when retRef() returns. Therefore, the reference returned by retRef() is useless. Most recent C++ compilers will flag it as an error when we try to return a reference to a local variable. The subscript operator An important use of returning a reference is found in overloading the subscript oper- ator. Consider the MyString class. Suppose that a is an object of MyString declared as follows. MyString a("Hello"); We know that, the above declaration creates object a having the C-String Hello as shown below. Suppose that we want to replace the character ’H’ in position with ’M’ , so that the modified C-String in a is Mello . We can accomplish this by adding a member function to the MyString class. The function declaration is as follows. char& operator(int); By this, we are overloading the unary operator . The name of the function is operator . It takes an int argument, which specifies the position of the element MyString Class Revisited 3 in the dynamic array pointed to by rep . Notice that the function returns a reference to a char . If this operator function is defined for the class, we can use any MyString object just as a character array. For example, the statements a[0] = ’M’; a[1] = ’E’; modify the C-String in object a to MEllo as illustrated below....
View Full Document

This note was uploaded on 04/07/2008 for the course CS 181 taught by Professor Satya during the Fall '08 term at Stevens.

Page1 / 12

Lect11_2 - MyString Class Revisited 1 Additional Class Features Part 2 MyString Class Revisited In this part we expand the MyString class adding

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online