Assume:
class Token
{
std::map<std::string, std::string> m_data;
public:
std::string& operator[](const char* c)
{return m_data[std::string(c)];}
};
int main()
{
// how to make this work:
Token t;
t["test"] = "Hello World";
t["test2"] = 12.345;
}
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);} 10 1342
There is no way to add double type variable to the map of string type
values. Only one solution is to covert double to string and than add
it. See sprintf/snprintf functions of the C standard library.
Gernot Frisch wrote: Assume:
class Token { std::map<std::string, std::string> m_data; public:
std::string& operator[](const char* c) {return m_data[std::string(c)];} };
int main() { // how to make this work: Token t; t["test"] = "Hello World"; t["test2"] = 12.345; }
Make operator [ ] return an object of a class that has two operator = , that
when used calls some function or operator in Token (by means of a reference
to the Token object that creates it, for example).
--
Salu2 Make operator [ ] return an object of a class that has two operator = , that when used calls some function or operator in Token (by means of a reference to the Token object that creates it, for example).
Like this?
class Token
{
....
Token& operator[](const char*]);
};
Token& operator = (Token& t, double d);
Token& operator = (Token& t, const char* p);
Gernot Frisch wrote: Make operator [ ] return an object of a class that has two operator = , that when used calls some function or operator in Token (by means of a reference to the Token object that creates it, for example).
Like this?
class Token { ... Token& operator[](const char*]); };
Token& operator = (Token& t, double d); Token& operator = (Token& t, const char* p);
IMO will be better to create another class, that way may allow to write
confusing code.
--
Salu2
Gernot Frisch wrote:
Make operator [ ] return an object of a class that has two operator = , that when used calls some function or operator in Token (by means of a reference to the Token object that creates it, for example).
Like this?
class Token { ... Token& operator[](const char*]); };
Token& operator = (Token& t, double d); Token& operator = (Token& t, const char* p);
Other than that operator= must be a member, this should work.
Btw: why const char*?
On 2005-06-29, Gernot Frisch <Me@Privacy.net> wrote: Assume:
class Token { std::map<std::string, std::string> m_data; public:
std::string& operator[](const char* c) {return m_data[std::string(c)];} };
int main() { // how to make this work: Token t; t["test"] = "Hello World"; t["test2"] = 12.345; }
Looks like you need a runtime-generic holder that can store different
types. Here's a sketch of what these things might look like:
class Thing {
// abstract conversion and cast operators
Thing* clone() const; // polymorphic copy
};
class Double : public Thing {
// concrete definitions
};
class String : public Thing {
// blah
};
then use a wrapper/smart pointer class to hold the thing
class SmartPtr {
Thing* impl;
public:
// operator overloads , use clone() to copy
then use a map< std::string, SmartPtr<Thing> >
Fortunately, the work has already been done for you, boost
includes a nice version of this generic container (the subclasses
are parametrized ... a clever fusion of runtime polymorphism and
templates)
map<std::string, boost::any> t;
t["test"] = std::string("Hello World");
t["test2"] = 12.345;
double d = boost::any_cast<double> ( t["test2"] );
Cheers,
--
Donovan Rebbechi http://pegasus.rutgers.edu/~elflord/
> Fortunately, the work has already been done for you, boost includes a nice version of this generic container (the subclasses are parametrized ... a clever fusion of runtime polymorphism and templates)
map<std::string, boost::any> t; t["test"] = std::string("Hello World"); t["test2"] = 12.345;
double d = boost::any_cast<double> ( t["test2"] );
boost::any! Thank you, that was what I was searching for. Other than that operator= must be a member, this should work. Btw: why const char*?
"Token" is in reality an XML node. I want to assign attributes to it
this way:
Node["ATTRIBUTE1"] = "Value 1";
Node["A_DOUBLE"] = 1.23;
Gernot Frisch wrote: Other than that operator= must be a member, this should work. Btw: why const char*?
"Token" is in reality an XML node. I want to assign attributes to it this way:
Node["ATTRIBUTE1"] = "Value 1"; Node["A_DOUBLE"] = 1.23;
Yes, but in your example, you stored std::string, so why not use it as
parameter too?
"Rolf Magnus" <ra******@t-online.de> schrieb im Newsbeitrag
news:da*************@news.t-online.com... Gernot Frisch wrote:
Other than that operator= must be a member, this should work. Btw: why const char*?
"Token" is in reality an XML node. I want to assign attributes to it this way:
Node["ATTRIBUTE1"] = "Value 1"; Node["A_DOUBLE"] = 1.23;
Yes, but in your example, you stored std::string, so why not use it as parameter too?
Sometimes you only get a const char* and then I think I had to
std::string(pChar) it before pasing to a function.
-Gernot This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Oliver Michael Milz |
last post by:
Hi *,
I have a class called r_Point with two operators defined as follows:
r_Range
r_Point::operator( r_Dimension i ) const throw( r_Eindex_violation )
{
return points;
}
|
by: Nimmi Srivastav |
last post by:
There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents a rather good
discussion of typecast operator overloading.
I am presenting...
|
by: c++newbie |
last post by:
Hi all,
I try to compile the following classes:
main.cpp:
#include <algorithm>
#include <iostream>
#include <fstream>
#include <iterator>
|
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: benben |
last post by:
Given a class template Vector<>, I would like to overload operator +.
But I have a hard time deciding whether the return type should be
Vector<U> or Vector<V>, as in:
template <typename U,...
|
by: allan.mcrae |
last post by:
I am having trouble with overloading the += operator when template
parameters are used. I have a class holding an array (called "derived"
in the following example) which derives from a base class...
|
by: Zilla |
last post by:
I have the following simple program. I just want to be able to do math
operations (+, -, =)on Timer sublcasses, but want to handle cases
where either rhs or lhs is an intrinsic value, However, the...
|
by: fabian.lim |
last post by:
Hi All,
Im a newbie to C++, I am trying to customize the vector template
STL to a template Class. My code is shown below. Im confused about
something and maybe somebody here might be able to...
|
by: fabian.lim |
last post by:
Hi all,
Im having a problem with my code. Im programming a vector class,
and am trying to overload the () operator in 2 different situations.
The first situation is to assign values, e.g. Y =...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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: 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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |