By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,856 Members | 1,986 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,856 IT Pros & Developers. It's quick & easy.

Overloading operator+

P: n/a
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[i] = cString[i];
itsString[itsLen]='\0';
}
-----
-----
String String::operator+(const String& rhs)
{
int totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i<itsLen; i++)
temp[i] = itsString[i];
for (j = 0, i = itsLen; j<rhs.GetLen(); j++, i++)
temp[i] = 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[i] = lhs[i];
for (j = 0, i = lhs.GetLen(); j<rhs.GetLen(); j++, i++)
temp[i] = 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

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 28 Jan 2005 07:23:23 -0800, wo*********@yahoo.com
<wo*********@yahoo.com> wrote:
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[i] = cString[i];
itsString[itsLen]='\0';
}
-----
-----
String String::operator+(const String& rhs)
{
int totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i<itsLen; i++)
temp[i] = itsString[i];
for (j = 0, i = itsLen; j<rhs.GetLen(); j++, i++)
temp[i] = 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[i] = lhs[i];
for (j = 0, i = lhs.GetLen(); j<rhs.GetLen(); j++, i++)
temp[i] = rhs[j];
temp[totalLen]='\0';
return temp;
}
-----------------

Now I am totally confused. How can this function accept two input
parameters ???
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::operator+(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?
--
have a nice day
ulrich
Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.