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: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
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: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