我有这样的代码C++ - 汽车铸件的std :: string
template <typename T>
class KeyValueProperty {
protected:
T value = T();
std::string key = "";
public:
KeyValueProperty(const std::string & key) : key(key) { }
T & operator = (const T &i) { return value = i; };
operator const T &(){ return value; };
};
struct T2 {
KeyValueProperty<std::string> x {"x"};
KeyValueProperty<double> y {"y"};
};
,并在主
T2 tx;
tx.x = "hellow";
tx.y = 10;
std::cout << static_cast<std::string>(tx.x) << ::std::endl;
std::cout << tx.y << ::std::endl;
这是正常工作。但是,这样做只是这
std::cout << tx.x << ::std::endl;
将在
错误C2679结束:二进制“< <”:没有操作员发现这需要类型的右手操作数‘测试:: KeyValueProperty’(或 没有可接受的转换)
是否有可能进行自动转换,或者我必须手动调用铸造?
但是,即使没有这个,为什么双重输出呢? –
@MartinPerry KeyValueProperty对T&隐式转换运算符。 –
但是,为什么这个隐式转换对于std :: string不起作用,如果它是用于double的呢? –