ReferenceReturn

ReferenceReturn - // to the output statement of p below....

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <string> using namespace std ; // We haven't discussed the syntax of s.length() yet. It // is calling a member function on s which returns the length // of the string s. return s.length() ; } // This example of returning a reference does not cause a problem. if ( stringLength(s1) > stringLength(s2) ) return s1 ; else return s2 ; } int main() { // The following (commented out) assignment works, but is an // incorrect use of references. // int p = pitfall(1,2) ; // This one does not work, and fails by providing garabage
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // to the output statement of p below. int & p = pitfall(1,2) ; string s1 ; string s2 ; s1 = "This is a relatively short sting." ; s2 = "This is a longer string, composed of " ; s2 = s2 + "many pieces, put together by the " ; s2 = s2 + "concatentation operator \"+\"." ; cout << "Of the two strings: " << endl << " " << s1 << endl << "and" << endl << " " << s2 << endl << endl ; cout << "The longest one is: " << endl << longestString (s1, s2) << endl ; cout << endl << endl ; cout << "Problems - p is: " << p << endl ; }...
View Full Document

This note was uploaded on 10/28/2010 for the course CSCI 1113 taught by Professor Staff during the Spring '08 term at Minnesota.

Ask a homework question - tutors are online