beautypg.com

HP Integrity NonStop J-Series User Manual

Page 49

background image

where() is an example of an overloaded operator. As you know, an overloaded operator is one
which can perform more than one function, depending on context or argument.

In the example, the function call operator RWCString::operator()is overloaded to take an argument
of type

RWCRegexp

, the regular expression. The operator returns either a substring that delimits

the regular expression, or a null substring if a matching expression cannot be found. The program
then calls the substring assignment operator, which replaces the delimited string with the contents
of the right hand side, or does nothing if this is the null substring. Because Tools.h++ provides the
overloaded operator, you can do a search and replace on the defined regular expression all in a
single line.

You will notice that you need two backlashes in "V[0-9]\\.[0-9]+" to indicate that the special
character "." is to be read literally as a decimal point. That's because the compiler removes one
backslash when it evaluates a literal string. The remaining backslash alerts the regular expression
evaluator to read whatever character follows literally.

In the next example,

RWCString

uses another overloaded operator, + , to concatenate the strings

s1 and s2. The toUpper member function converts the strings from lower to upper case, and the
results are sent to cout:

RWCString s1, s2;
cin >> s1 >> s2;
cout << toUpper(s1+s2);

See the Class Reference for details on the string classes.

This manual is related to the following products: