[英]Integer c++ wrapper
I'm doing a very small and simple Integer class wrapper in C++, which globaly looks like this: 我正在用C ++做一个非常小和简单的Integer类包装器,其全局性如下所示:
class Int
{
...
private:
int value;
...
}
I handled almost all the possible assignements, but I don't find out what kind of operator I have to use to get native left assignement. 我处理了几乎所有可能的分配,但是我不知道要使用本地左分配必须使用哪种运算符。
eg: 例如:
Int myInteger(45);
int x = myInteger;
You might want a conversion operator to convert to int: 您可能希望转换运算符转换为int:
class Int
{
public:
operator int() const { return value; }
...
};
This allows the following initialization of an int
这允许以下
int
初始化
int x = myInteger;
In C++11, you can decide whether you restrict that conversion to int
, or whether you allow further conversions from int
to something else. 在C ++ 11中,您可以决定是否将转换限制为
int
,或者是否允许从int
到其他内容的进一步转换。 To restrict to int
, use an explicit
conversion operator: 要限制为
int
,请使用explicit
转换运算符:
explicit operator int() const { return value; }
although it is probably not necessary in this case. 尽管在这种情况下可能没有必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.