我有一个类别,只有数据成员,即32个点名未经签名的分类。 也就是说,这一类人认为:
class A {
protected:
unsigned int bits;
/* ... some methods ... */
public:
/* ... some methods ... */
};
但是,我也希望能够从32个没有签名的ger户中暗地皈依。 因此,我在一份影印构件中加上了一台投稿人。
class A {
protected:
unsigned int bits;
/* ... some methods ... */
public:
A(): bits(0) {} // default constructor
A(const unsigned int i): bits(i) {} // convert from unsigned int to A
operator unsigned int() { return bits; } // convert from A to unsigned int
/* ... some methods ... */
};
例如,我现在可以做以下工作:
int main () {
// default constructor
A a0 = A();
// convert from unsigned int
A a1 = 1 + 2;
// conversions between the two types:
unsigned int result = (a0 & a1 + 5) / 2;
}
然而,我却在努力使其工作达到<条形状>。 尤其是,以下工作:
int main () {
const A a = 5; // works
unsigned int i = a; // compiletime error
}
It says "No suitable conversion function from const A to unsigned int exists.". I m using Visual Studio 2010 with the default compiler as provided by microsoft.
我需要创造哪些转换功能,以便适当开展工作?