473,396 Members | 1,815 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,396 software developers and data experts.

operator with differing return type?

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);}

Jul 23 '05 #1
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.

Jul 23 '05 #2
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
Jul 23 '05 #3

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);

Jul 23 '05 #4
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
Jul 23 '05 #5
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*?

Jul 23 '05 #6
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/
Jul 23 '05 #7
>
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.
Jul 23 '05 #8
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;
Jul 23 '05 #9
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?

Jul 23 '05 #10

"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
Jul 23 '05 #11

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

Similar topics

3
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; }
2
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...
6
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>
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 &)
17
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,...
2
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...
11
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...
4
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...
1
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 =...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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...
0
tracyyun
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...

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.