运营商有不同的退货类型? [英] operator with differing return type?

查看:62
本文介绍了运营商有不同的退货类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设:


类令牌

{

std :: map< std :: string,std :: string> ; m_data;

public:


std :: string& operator [](const char * c)

{return m_data [std :: string(c)];}

};


int main()

{

//如何使这项工作:

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

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

推荐答案

无法将双类型变量添加到字符串类型的地图

值。只有一个解决方案是将双重转换为字符串而不是添加

它。请参阅C标准库的sprintf / snprintf函数。

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写道:
Gernot Frisch wrote:
假设: {
std :: map< std :: string,std :: string> m_data;
公开:

std :: string& operator [](const char * c)
{return m_data [std :: string(c)];}
};

int main()
{
//如何使这项工作:
令牌t;
t [" test"] =" Hello World";
t [" test2"] = 12.345 ;
}
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;
}




使operator []返回一个具有两个operator =的类的对象,使用时调用

Token中的一些函数或操作符(例如,通过引用

到创建它的Token对象)。


-

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

--
Salu2



让operator []返回一个有两个运算符的类的对象
=,
在使用时会调用令牌中的某些函数或运算符(例如,通过对创建它的Token对象的引用)。
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).




喜欢这个?


班级代币

{

....

Token& operator [](const char *]);

};


Token& operator =(Token& t,double d);

Token& operator =(Token& t,const char * p);



Like this?

class Token
{
....
Token& operator[](const char*]);
};

Token& operator = (Token& t, double d);
Token& operator = (Token& t, const char* p);


这篇关于运营商有不同的退货类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆