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? 10 1726
AFAIK,
void myClass::operator=(std::string& str);
Whoops.. that should probably be:
myClass& myClass::operator=(std::string& str)
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.
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.
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.
"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
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
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...
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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 &)
|
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 '<'...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |