473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

operator= ?

I want to have a method in my class that will set its value from a
right hand side thats a std::string

ie..

myClass c;
std::string x("Hello");
c = x;

what method must I implement in myclass to do this?

Mar 21 '06 #1
10 1726
Cat
AFAIK,

void myClass::operator=(std::string& str);

Mar 21 '06 #2
Cat
Whoops.. that should probably be:

myClass& myClass::operator=(std::string& str)

Mar 21 '06 #3

Cat wrote:
Whoops.. that should probably be:

myClass& myClass::operator=(std::string& str)


Or better yet,

myClass& myClass::operator=(const std::string& str);

As I assume the OP does not need to modify str.

Mar 21 '06 #4
JustSomeGuy wrote:
I want to have a method in my class that will set its value from a
right hand side thats a std::string

ie..

myClass c;
std::string x("Hello");
c = x;

what method must I implement in myclass to do this?

myClass& operator=(const std::string);

--
Ian Collins.
Mar 21 '06 #5
In article <11**********************@z34g2000cwc.googlegroups .com>,
fe****************@gmail.com wrote:
Cat wrote:
Whoops.. that should probably be:

myClass& myClass::operator=(std::string& str)


Or better yet,

myClass& myClass::operator=(const std::string& str);

As I assume the OP does not need to modify str.


In that vain. Is there a table somewhere on the net that shows the
standard operator signatures?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 21 '06 #6
"Daniel T." <po********@earthlink.net> wrote in message
news:po******************************@news.east.ea rthlink.net
In article <11**********************@z34g2000cwc.googlegroups .com>,
fe****************@gmail.com wrote:
Cat wrote:
Whoops.. that should probably be:

myClass& myClass::operator=(std::string& str)


Or better yet,

myClass& myClass::operator=(const std::string& str);

As I assume the OP does not need to modify str.


In that vain. Is there a table somewhere on the net that shows the
standard operator signatures?
--
Magic depends on tradition and belief. It does not welcome
observation, nor does it profit by experiment. On the other hand,
science is based on experience; it is open to correction by
observation and experiment.


I don't know what you mean by "standard operator signatures", but Vol I of
Bruce Eckel's Thinking in C++ has a chapter showing how to define every
user-definable operator. It is Chapter 12.

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
--
John Carson
Mar 21 '06 #7
In message <11***************@drone2-svc-skyt.qsi.net.nz>, Ian Collins
<ia******@hotmail.com> writes
JustSomeGuy wrote:
I want to have a method in my class that will set its value from a
right hand side thats a std::string
ie..
myClass c;
std::string x("Hello");
c = x;
what method must I implement in myclass to do this?

myClass& operator=(const std::string);

What's the "const" for?

--
Richard Herring
Mar 27 '06 #8
Richard Herring wrote:
In message <11***************@drone2-svc-skyt.qsi.net.nz>, Ian Collins
<ia******@hotmail.com> writes
JustSomeGuy wrote:
I want to have a method in my class that will set its value from a
right hand side thats a std::string
ie..
myClass c;
std::string x("Hello");
c = x;
what method must I implement in myclass to do this?

myClass& operator=(const std::string);

What's the "const" for?


He probably meant:

myClass& operator=(const std::string&)

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 27 '06 #9
Ben Pope wrote:
Richard Herring wrote:
In message <11***************@drone2-svc-skyt.qsi.net.nz>, Ian Collins
<ia******@hotmail.com> writes
JustSomeGuy wrote:

I want to have a method in my class that will set its value from a
right hand side thats a std::string
ie..
myClass c;
std::string x("Hello");
c = x;
what method must I implement in myclass to do this?

myClass& operator=(const std::string);

What's the "const" for?

He probably meant:

myClass& operator=(const std::string&)

In this case, no. At least with the STL I use it's more efficient to
pass strings by value....

To the OP - it's const because it's value isn't changed in the function.

--
Ian Collins.
Mar 27 '06 #10
In message <11***************@drone2-svc-skyt.qsi.net.nz>, Ian Collins
<ia******@hotmail.com> writes
Ben Pope wrote:
Richard Herring wrote:
In message <11***************@drone2-svc-skyt.qsi.net.nz>, Ian Collins
<ia******@hotmail.com> writes

JustSomeGuy wrote:

> I want to have a method in my class that will set its value from a
> right hand side thats a std::string
> ie..
> myClass c;
> std::string x("Hello");
> c = x;
> what method must I implement in myclass to do this?
>
myClass& operator=(const std::string);

What's the "const" for?

He probably meant:

myClass& operator=(const std::string&)

In this case, no. At least with the STL I use it's more efficient to
pass strings by value....


Measurably?
To the OP - it's const because it's value isn't changed in the function.

(a) How do you know? It's his function, not yours ;-)

(b) It's implementation detail which has no place in a function
declaration, since it doesn't affect the function's signature.

--
Richard Herring
Mar 28 '06 #11

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.