 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
wongjoekmeu@yahoo.com Guest
|
Posted: Fri Jan 28, 2005 3:23 pm Post subject: Overloading operator+ |
|
|
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
itsString[i] = cString[i];
itsString[itsLen]=' ';
}
-----
-----
String String::operator+(const String& rhs)
{
int totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i
temp[i] = itsString[i];
for (j = 0, i = itsLen; j
temp[i] = rhs[j];
temp[totalLen]=' ';
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
temp[i] = lhs[i];
for (j = 0, i = lhs.GetLen(); j
temp[i] = rhs[j];
temp[totalLen]=' ';
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
|
|
| Back to top |
|
 |
ulrich Guest
|
Posted: Fri Jan 28, 2005 3:44 pm Post subject: Re: Overloading operator+ |
|
|
On 28 Jan 2005 07:23:23 -0800, [email]wongjoekmeu (AT) yahoo (DOT) com[/email]
<wongjoekmeu (AT) yahoo (DOT) com> wrote:
| Quote: | 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
#include
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
itsString[i] = cString[i];
itsString[itsLen]=' ';
}
-----
-----
String String::operator+(const String& rhs)
{
int totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
int i, j;
for (i = 0; i
temp[i] = itsString[i];
for (j = 0, i = itsLen; j
temp[i] = rhs[j];
temp[totalLen]=' ';
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.
| Quote: | 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
temp[i] = lhs[i];
for (j = 0, i = lhs.GetLen(); j
temp[i] = rhs[j];
temp[totalLen]=' ';
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 :)
| Quote: | 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
|
|
| Back to top |
|
 |
Matthew Guest
|
Posted: Fri Jan 28, 2005 4:28 pm Post subject: Re: Overloading operator+ |
|
|
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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|