473,698 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how i can write "operator+" ?

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
Jul 29 '06 #1
8 1886
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;
}

--
Ian Collins.
Jul 29 '06 #2
"av" <av@ala.awrot e in message
news:6g******** *************** *********@4ax.c om...
>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

:)
Jul 29 '06 #3
av
On Sat, 29 Jul 2006 18:44:01 +1200, Ian Collins wrote:
>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
Jul 29 '06 #4
av
On Sat, 29 Jul 2006 18:44:01 +1200, Ian Collins wrote:
>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+(cha r*, 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

Jul 29 '06 #5
av wrote:
>>
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.
Jul 29 '06 #6
av
On Sat, 29 Jul 2006 10:41:55 +0200, av wrote:
>On Sat, 29 Jul 2006 18:44:01 +1200, Ian Collins wrote:
>>i have my little string class, now i want do define something like
....
>>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 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"?
Jul 29 '06 #7
av wrote:
On Sat, 29 Jul 2006 10:41:55 +0200, av wrote:
>>On Sat, 29 Jul 2006 18:44:01 +1200, Ian Collins wrote:
>>>>i have my little string class, now i want do define something like

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

--
Ian Collins.
Jul 29 '06 #8

"av" <av@ala.askre v i meddelandet
news:5j******** *************** *********@4ax.c om...
On Sat, 29 Jul 2006 10:41:55 +0200, av wrote:
>>On Sat, 29 Jul 2006 18:44:01 +1200, Ian Collins wrote:
>>>i have my little string class, now i want do define something
like
...
>>>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 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
Jul 29 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
6451
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
9
2197
by: Emanuele Aina | last post by:
I have some code which does a lot of "in" on lists containing objects with no __eq__ defined. It all goes fast until I add the __lt__() method: then I have a slowdown comparable to the one I get using the overridden __eq__, while the __lt__ method is never called. Someone can explain me why?
2
2757
by: John Goche | last post by:
Hello, Could anyone please provide with some information on the C++ overloaded cast operator and in which circumstances this might be useful? I have consulted several references but found no information on the uses of this operator. Thanks, JG
2
1446
by: tomy | last post by:
Hi All: I just wonder what happened when i use operator "=" to construct an obj, as following: class Obj { public: Obj(); Obj& operator = (const Obj &); }
5
2588
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to determine wether a class contains a member. My understanding is that if a template function has an error during resolution of the function types, it is quietly eliminated from the resolution process. This can be used to detect things that would...
14
2802
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
2
2013
by: babakandme | last post by:
Hi everybody:D I've a string that contains the name of a class. Some members told that I can use """Stringizing Operator (#)""", but the problem is here, that I have the string, & I want something vice- versa. As we know with """Stringizing Operator (#)""", we can get the stirng name of a class or ... str <--- #ClassA
7
3041
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but surely this is possible? I could define an operator<< but for various reasons, I really want to convert to a 'const char*' (to embed into a string which becomes part of a window's caption, etc).
3
35865
by: tvnaidu | last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?. Main.cpp:377: undefined reference to `operator delete(void*)' tinyXML/include/tinystr.h:259: undefined reference to `operator delete(void*)' ../common/tinyXML/include/tinyxml.h:1401: undefined reference to `operator delete(void*)'...
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.