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 ...

Get Navigating C++ and Object-Oriented Design now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.