operator with differing return type?

G

Gernot Frisch

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

SnaiL

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.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Gernot said:
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).
 
G

Gernot Frisch

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

=?ISO-8859-15?Q?Juli=E1n?= Albo

Gernot said:
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.
 
R

Rolf Magnus

Gernot said:
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*?
 
D

Donovan Rebbechi

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,
 
G

Gernot Frisch

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.
 
G

Gernot Frisch

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

Rolf Magnus

Gernot said:
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?
 
G

Gernot Frisch

Rolf Magnus said:
Gernot said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top