Question about operator overload

I am trying to understand a program that includes the following definition for a function f

void f(String S, const String& r)
{

}

Here String in the argument stands for the class. I am confused by the difference between the definitions of these two arguments: "String S" and "const String & r". S should represent an object of class String, then what about r?

In more detail, f is defined as

void f(String S, const String& r)
{
   int c1 = S[1];  // c1=s.operator[](1).operator char( )
   s[1] ='c';      // s.operator[](1).operator=('c')

   int c2 = r[1];  // c2 = r.operator[](1)
   r[1] = 'd';     // error: assignment to char, r.operator[](1) = 'd'      
 }

This piece of code shows how the operator is overloaded, but these comments do not help me much. For example, why r [1] = 'd' is not correct? Thanks for helping me figure this out.

+3
source share
2 answers

const String& r - String r. r , String. , , , S , . , r ( ).

: r ( const), S.

const , f r.

: https://isocpp.org/wiki/faq/references#overview-refs

+8

, , , .

, , - . , .

0

Source: https://habr.com/ru/post/1796728/


All Articles