 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
av Guest
|
Posted: Sat Jul 29, 2006 9:10 am Post subject: how i can write "operator+"? |
|
|
i have my little string class, now i want do define something like
this [ char *n="terza";
sstring a;
a= "prima " + "seconda " + n; ]
but for doing it i have to write
sstream& operator+(char* a, char* b){}
but compiler say something like
"operator+(char*, char*) must be a member function or have a parameter
of class type"
how can i write a class type "string" that allow to write
a= "prima " + "seconda " + n; above
thank you |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sat Jul 29, 2006 9:10 am Post subject: Re: how i can write "operator+"? |
|
|
av wrote:
| Quote: |
sstring operator+( const sstring& a, const sstring& b )
{
sstring temp( a );
temp += b;
return temp;
}
but
temp should have the lifetime of operator+
this could be ok if it is "static" something like
static sstring temp( a );
but in this case something strange have to happen in
a = ((a="prima")+"s") + ((b="seconda")+"h");
but i have one simple solution for all this
Please capitalise correctly. |
What exactly do you mean by "but temp should have the lifetime of
operator+"?
If I understand you correctly, it does and note operator+ returns by value.
--
Ian Collins. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sat Jul 29, 2006 9:10 am Post subject: Re: how i can write "operator+"? |
|
|
av wrote:
| Quote: | i have my little string class, now i want do define something like
this [ char *n="terza";
sstring a;
a= "prima " + "seconda " + n; ]
but for doing it i have to write
sstream& operator+(char* a, char* b){}
Why sstream? |
| Quote: | but compiler say something like
"operator+(char*, char*) must be a member function or have a parameter
of class type"
how can i write a class type "string" that allow to write
a= "prima " + "seconda " + n; above
thank you
|
You can't.
You could write
sstring a = "prima ";
a = a + "seconda " + n;
Given the correct operators on sstring, but your code requires there to
be an operator +() for const char*
The common way to implement operator +() is to give your class a member
operator +=() and use this in a non-member operator +():
class sstring
{
// stuff
sstring& operator+=( const sstring& );
};
sstring operator+( const sstring& a, const sstring& b )
{
sstring temp( a );
temp += b;
return temp;
}
--
Ian Collins. |
|
| Back to top |
|
 |
Luc The Perverse Guest
|
Posted: Sat Jul 29, 2006 9:10 am Post subject: Re: how i can write "operator+"? |
|
|
"av" <av (AT) ala (DOT) a> wrote in message
news:6g0mc211sbcjs2fan1tmtk8v6l9gog94mj (AT) 4ax (DOT) com...
| Quote: | i have my little string class, now i want do define something like
this [ char *n="terza";
sstring a;
a= "prima " + "seconda " + n; ]
but for doing it i have to write
sstream& operator+(char* a, char* b){}
but compiler say something like
"operator+(char*, char*) must be a member function or have a parameter
of class type"
how can i write a class type "string" that allow to write
a= "prima " + "seconda " + n; above
thank you
|
You can't over ride operators for primitives :)
--
LTP
:) |
|
| Back to top |
|
 |
av Guest
|
Posted: Sat Jul 29, 2006 9:11 am Post subject: Re: how i can write "operator+"? |
|
|
On Sat, 29 Jul 2006 18:44:01 +1200, Ian Collins wrote:
| Quote: | av wrote:
i have my little string class, now i want do define something like
this [ char *n="terza";
sstring a;
a= "prima " + "seconda " + n; ]
but for doing it i have to write
sstream& operator+(char* a, char* b){}
Why sstream?
but compiler say something like
"operator+(char*, char*) must be a member function or have a parameter
of class type"
how can i write a class type "string" that allow to write
a= "prima " + "seconda " + n; above
thank you
You can't.
You could write
sstring a = "prima ";
a = a + "seconda " + n;
Given the correct operators on sstring, but your code requires there to
be an operator +() for const char*
The common way to implement operator +() is to give your class a member
operator +=() and use this in a non-member operator +():
class sstring
{
// stuff
sstring& operator+=( const sstring& );
};
sstring operator+( const sstring& a, const sstring& b )
{
sstring temp( a );
temp += b;
return temp;
}
|
but
temp should have the lifetime of operator+
this could be ok if it is "static" something like
static sstring temp( a );
but in this case something strange have to happen in
a = ((a="prima")+"s") + ((b="seconda")+"h");
but i have one simple solution for all this |
|
| Back to top |
|
 |
av Guest
|
Posted: Sat Jul 29, 2006 9:11 am Post subject: Re: how i can write "operator+"? |
|
|
On Sat, 29 Jul 2006 18:44:01 +1200, Ian Collins wrote:
| Quote: | av wrote:
i have my little string class, now i want do define something like
this [ char *n="terza";
sstring a;
a= "prima " + "seconda " + n; ]
but for doing it i have to write
sstream& operator+(char* a, char* b){}
Why sstream?
thank you to all, |
right the sstring above is wrong it should be sstream |
|
| 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
|
|