Connecting Tech Pros Worldwide Help | Site Map

operator= ?

JustSomeGuy
Guest
 
Posts: n/a
#1: Mar 21 '06
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?

Cat
Guest
 
Posts: n/a
#2: Mar 21 '06

re: operator= ?


AFAIK,

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

Cat
Guest
 
Posts: n/a
#3: Mar 21 '06

re: operator= ?


Whoops.. that should probably be:

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

ferdinand.stefanus@gmail.com
Guest
 
Posts: n/a
#4: Mar 21 '06

re: operator= ?



Cat wrote:[color=blue]
> Whoops.. that should probably be:
>
> myClass& myClass::operator=(std::string& str)[/color]

Or better yet,

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

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

Ian Collins
Guest
 
Posts: n/a
#5: Mar 21 '06

re: operator= ?


JustSomeGuy wrote:[color=blue]
> 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?
>[/color]
myClass& operator=(const std::string);

--
Ian Collins.
Daniel T.
Guest
 
Posts: n/a
#6: Mar 21 '06

re: operator= ?


In article <1142915688.571665.109880@z34g2000cwc.googlegroups .com>,
ferdinand.stefanus@gmail.com wrote:
[color=blue]
> Cat wrote:[color=green]
> > Whoops.. that should probably be:
> >
> > myClass& myClass::operator=(std::string& str)[/color]
>
> Or better yet,
>
> myClass& myClass::operator=(const std::string& str);
>
> As I assume the OP does not need to modify str.[/color]

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.
John Carson
Guest
 
Posts: n/a
#7: Mar 21 '06

re: operator= ?


"Daniel T." <postmaster@earthlink.net> wrote in message
news:postmaster-930859.07520721032006@news.east.earthlink.net[color=blue]
> In article <1142915688.571665.109880@z34g2000cwc.googlegroups .com>,
> ferdinand.stefanus@gmail.com wrote:
>[color=green]
>> Cat wrote:[color=darkred]
>>> Whoops.. that should probably be:
>>>
>>> myClass& myClass::operator=(std::string& str)[/color]
>>
>> Or better yet,
>>
>> myClass& myClass::operator=(const std::string& str);
>>
>> As I assume the OP does not need to modify str.[/color]
>
> 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.[/color]

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


Richard Herring
Guest
 
Posts: n/a
#8: Mar 27 '06

re: operator= ?


In message <1142918073.602875@drone2-svc-skyt.qsi.net.nz>, Ian Collins
<ian-news@hotmail.com> writes[color=blue]
>JustSomeGuy wrote:[color=green]
>> 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?
>>[/color]
>myClass& operator=(const std::string);
>[/color]
What's the "const" for?

--
Richard Herring
Ben Pope
Guest
 
Posts: n/a
#9: Mar 27 '06

re: operator= ?


Richard Herring wrote:[color=blue]
> In message <1142918073.602875@drone2-svc-skyt.qsi.net.nz>, Ian Collins
> <ian-news@hotmail.com> writes[color=green]
>> JustSomeGuy wrote:[color=darkred]
>>> 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?
>>>[/color]
>> myClass& operator=(const std::string);
>>[/color]
> What's the "const" for?[/color]

He probably meant:

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

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Ian Collins
Guest
 
Posts: n/a
#10: Mar 27 '06

re: operator= ?


Ben Pope wrote:[color=blue]
> Richard Herring wrote:
>[color=green]
>> In message <1142918073.602875@drone2-svc-skyt.qsi.net.nz>, Ian Collins
>> <ian-news@hotmail.com> writes
>>[color=darkred]
>>> 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);
>>>[/color]
>> What's the "const" for?[/color]
>
>
> He probably meant:
>
> myClass& operator=(const std::string&)
>[/color]
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.
Richard Herring
Guest
 
Posts: n/a
#11: Mar 28 '06

re: operator= ?


In message <1143496607.445875@drone2-svc-skyt.qsi.net.nz>, Ian Collins
<ian-news@hotmail.com> writes[color=blue]
>Ben Pope wrote:[color=green]
>> Richard Herring wrote:
>>[color=darkred]
>>> In message <1142918073.602875@drone2-svc-skyt.qsi.net.nz>, Ian Collins
>>> <ian-news@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?[/color]
>>
>>
>> He probably meant:
>>
>> myClass& operator=(const std::string&)
>>[/color]
>In this case, no. At least with the STL I use it's more efficient to
>pass strings by value....[/color]

Measurably?[color=blue]
>
>To the OP - it's const because it's value isn't changed in the function.
>[/color]
(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
Closed Thread