O'Reilly logo

Navigating C++ and Object-Oriented Design by Gail Anderson, Paul Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.5. Putting It All Together

Lvalue and Rvalue Separation

The String class in this chapter provides an overloaded operator()() for substrings as rvalues only. Recall that the notation s(position,count) is a substring of length count characters, starting with position in String s (position starts at 0). Now let's look at an example with substrings as lvalues and rvalues.

String s1 = "alphabet"; 
cout << s1(5,3) << endl;         // rvalue - substring "bet"
s1(5,3) = "ray";                 // lvalue - s1 is now "alpharay"

We also need to make lvalues behave properly for constant substrings.

const String s2 = "alphabet";    // constant String 
cout << s2(5,3) << endl;         // rvalue - substring "bet"
s2(5,3) = "ray";                 // lvalue - error, s2 is constant

Substrings are interesting ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required