C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how i can write "operator+"?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
av
Guest





PostPosted: Sat Jul 29, 2006 9:10 am    Post subject: how i can write "operator+"? Reply with 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
Back to top
Ian Collins
Guest





PostPosted: Sat Jul 29, 2006 9:10 am    Post subject: Re: how i can write "operator+"? Reply with quote



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





PostPosted: Sat Jul 29, 2006 9:10 am    Post subject: Re: how i can write "operator+"? Reply with quote



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





PostPosted: Sat Jul 29, 2006 9:10 am    Post subject: Re: how i can write "operator+"? Reply with quote

"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





PostPosted: Sat Jul 29, 2006 9:11 am    Post subject: Re: how i can write "operator+"? Reply with quote

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





PostPosted: Sat Jul 29, 2006 9:11 am    Post subject: Re: how i can write "operator+"? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.