Overloading operator+

W

wongjoekmeu

Hello All,
I have a question about a C++ listing that I don't understand from a
book that I use to learn C++. In the listing a class String is declared
and defined. The beginning look like this
----------
#include <iostream>
#include <string.h>
using namespace std;

Class String
{
public:
String();
String(const char *const);
---
---
String operator+(const String&);
}
-----------
The second constructor {String(const char* const)} and the overloaded
operator+ are defined as follow:
-----------
// Converts a character array to a String
String::String(const char * const cString)
{
itsLen = strlen(cString);
itsString = new char[itsLen+1];
for (int i = 0; i<itsLen; i++)
itsString = cString;
itsString[itsLen]='\0';
}
-----
-----
String String::eek:perator+(const String& rhs)
{
int totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i<itsLen; i++)
temp = itsString;
for (j = 0, i = itsLen; j<rhs.GetLen(); j++, i++)
temp = rhs[j];
temp[totalLen]='\0';
return temp;
}
-----

Now the explanation of the listing in the book says that you can
declare a C-style string as
char cString[] = {"hello world");
and a String as
String sString(" world"); since the constructor will covert the C-style
string to a type String. But it says that

String sStringTwo = cString + sString ;
will result in to an error since the overloaded operator+ function is
not a member function of a C-style string, which I can understand.
Moreover we could read this line as
cString.operator+(sString).

However if you change the sequence
String sStringTwo = sString + cString ;
seems to work. Since sString.operator+(cString) seems to be a member of
the class String.
My first question is that if the operator+ is expecting a reference to
a String. Why should this work since you pass a C-style string into it.
How can this be possible ???

Now the book explain that in order to make
String sStringTwo = cString + sString ;
to work one can add a friend function and overload the operator+
function as follow:
----------
class String
{
----
----
friend String operator+(const String&, const String&) ;
----
}
-----
-----
String operator+(const String& lhs, const String& rhs)
{
int totalLen = lhs.GetLen() + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i<lhs.GetLen(); i++)
temp = lhs;
for (j = 0, i = lhs.GetLen(); j<rhs.GetLen(); j++, i++)
temp = rhs[j];
temp[totalLen]='\0';
return temp;
}
-----------------

Now I am totally confused. How can this function accept two input
parameters ???
How will this overloaded operator+ function knows that it is being
called ??
If the first operator+ function can be read as
sString.operator+(cString), how do I read the overloaded function in
this form. As to my opinion after the "+" operator there is only one
input parameter. But again, if it can have to argument. At least one
argument is of type C-style string, how can this be an input parameter
for the overloaded function which requires a const String & ????
Many thanks in advance for your help.

Robert
 
U

ulrich

Hello All,
I have a question about a C++ listing that I don't understand from a
book that I use to learn C++. In the listing a class String is declared
and defined. The beginning look like this
----------
#include <iostream>
#include <string.h>
using namespace std;

Class String
{
public:
String();
String(const char *const);
---
---
String operator+(const String&);
}
-----------
The second constructor {String(const char* const)} and the overloaded
operator+ are defined as follow:
-----------
// Converts a character array to a String
String::String(const char * const cString)
{
itsLen = strlen(cString);
itsString = new char[itsLen+1];
for (int i = 0; i<itsLen; i++)
itsString = cString;
itsString[itsLen]='\0';
}
-----
-----
String String::eek:perator+(const String& rhs)
{
int totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i<itsLen; i++)
temp = itsString;
for (j = 0, i = itsLen; j<rhs.GetLen(); j++, i++)
temp = rhs[j];
temp[totalLen]='\0';
return temp;
}
-----

Now the explanation of the listing in the book says that you can
declare a C-style string as
char cString[] = {"hello world");
and a String as
String sString(" world"); since the constructor will covert the C-style
string to a type String. But it says that

String sStringTwo = cString + sString ;
will result in to an error since the overloaded operator+ function is
not a member function of a C-style string, which I can understand.
Moreover we could read this line as
cString.operator+(sString).

However if you change the sequence
String sStringTwo = sString + cString ;
seems to work. Since sString.operator+(cString) seems to be a member of
the class String.
My first question is that if the operator+ is expecting a reference to
a String. Why should this work since you pass a C-style string into it.
How can this be possible ???


implicit conversion, i'd say.
i.e. implicit use of String::String(const char * const cString) to convert
the const char* to a String
try ot write "explicit String::String(const char * const cString)" and see
what happens.

Now the book explain that in order to make
String sStringTwo = cString + sString ;
to work one can add a friend function and overload the operator+
function as follow:
----------
class String
{
----
----
friend String operator+(const String&, const String&) ;
----
}
-----
-----
String operator+(const String& lhs, const String& rhs)
{
int totalLen = lhs.GetLen() + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i<lhs.GetLen(); i++)
temp = lhs;
for (j = 0, i = lhs.GetLen(); j<rhs.GetLen(); j++, i++)
temp = rhs[j];
temp[totalLen]='\0';
return temp;
}


this is, as you correctly say, a (free) function and not a method / member
function of class String.
thus, you need to supply the lhs and the rhs, in contrast to
String::eek:perator+(const String& rhs), where the lhs is the object x (of
type String) calling the operator: x.operator+(rhs), or: x + rhs :)

How will this overloaded operator+ function knows that it is being
called ??
If the first operator+ function can be read as
sString.operator+(cString), how do I read the overloaded function in
this form. As to my opinion after the "+" operator there is only one
input parameter. But again, if it can have to argument. At least one
argument is of type C-style string, how can this be an input parameter
for the overloaded function which requires a const String & ????

implicit conversion?
 
M

Matthew

The compiler contructs a temporary String object using the String(const
char *const) constructor and then binds that temporary to the parameter
in String operator+(const String&). This is only possible because the
parameter to operator+ is a reference to a const reference. This ensure
that the temporary cannot be passed back to the caller (since it will
immediately be destroyed when the function returns). It's illegal to
pass a temporary object to a non-constant reference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top