English 中文(简体)
C++中从用户界定的组合类别转向建筑类型
原标题:Converting from a const user-defined class to a builtin type in C++

我有一个类别,只有数据成员,即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.

我需要创造哪些转换功能,以便适当开展工作?

最佳回答

Declare the conversion operator const:

operator unsigned int() const { return bits; }

洞穴:合法皈依业者几乎总是不好。 危险通常远远大于要求履行老旧成员职责的轻微不便:

unsigned int asInt() const { return bits; }
⋮
unsigned int i = a.asInt();

The ultimate solution, if you have C++11, is to declare an explicit conversion operator:

explicit operator unsigned int() const { return bits; }
⋮
unsigned int i = static_cast<int>(a);
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签