how i can write "operator+"?

A

av

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
 
L

Luc The Perverse

av said:
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 :)
 
I

Ian Collins

av said:
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;
}
 
A

av

av said:
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
 
A

av

av said:
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
 
I

Ian Collins

av said:
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.
 
A

av

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

i did read it wrong like
"sstring& operator+( const sstring& a, const sstring& b )"
but in the above case

a= a+"1"+"2"+"3"+"4";

it seems to me there is a copy more in each "+";
and in that case what is it the lifetime of "temp"?
 
I

Ian Collins

av said:
i did read it wrong like
"sstring& operator+( const sstring& a, const sstring& b )"
but in the above case

a= a+"1"+"2"+"3"+"4";

it seems to me there is a copy more in each "+";
and in that case what is it the lifetime of "temp"?

What are you asking? I shall give up on this if you don't make the
effort to capitalise...
 
B

Bo Persson

av said:
i did read it wrong like
"sstring& operator+( const sstring& a, const sstring& b )"
but in the above case

a= a+"1"+"2"+"3"+"4";

it seems to me there is a copy more in each "+";

Yes. The compiler's optimizer has some work to do here!
and in that case what is it the lifetime of "temp"?

The temp itself just lives inside the operator. Its lifetime ends
after the return statement.

But, as the operator returns by value, the returned string lives until
the end of the enclosing expression. That's at the semicolon after the
"4".


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top