English 中文(简体)
2. 如何界定某一类别中的列举,并从外部加以使用
原标题:how to define an enumeration inside a class and use it from the outside

仅仅因为我没有确切知道在我的“++”书上或从山的角度研究这一问题。 我如何在课堂内实际界定一些插图(此处为{左=1,右=2,顶=3,底层=4)。 我希望能够把这一计数作为成员职能的一个参数,而不是一个分类,从而利用外部的计数......

www.un.org/Depts/DGACM/index_spanish.htm 我是否能够这样做,或者我能以更好的方式把清单具体化到那个类别?

这里指不工作的法典:enum my Sprite:<u name> myy Sprite:side member my Sprite “my Sprite:side”不是“,原因有:

class mySprite : public sf::Sprite
{
public:
    enum{left=1, right=2, top=3, bottom=4} side;

    float GetSide(side aSide){
        switch(aSide){
            // do stuff
        }
    };
};
最佳回答

The simplest change needed to get your code to work is this:

class mySprite : public sf::Sprite
{
public:
    enum side{ left=1, right=2, top=3, bottom=4 };

    float GetSide(side aSide)
    {
        switch(aSide)
        {
            // do stuff
            // add breaks; as appropriate
            case left:
            case right:
            case top:
            case bottom:
        }
    }
};

也可以这样做:

    typedef enum {left = 1, right, top, bottom} side;

系指为您的<代码>my Sprite/code>类别界定匿名体,使<代码>在栏内成为有效完成与上述代码相同内容的物。 仅就湿度而言,首先需要分配起立体。 这一点之后产生的所有价值,除非你明确指定其他价值,否则每次都会增加1个。

问题回答

我认为,这个例子说明:

class A {
public:
    enum directions { top, left, right, bottom }; // directions is the type
                                                  // of the enum elements

    void f(directions dir) {
        if (dir == left) {
            // ...
        } else {
            // ...
        }
    }
};

A object;
object.f(A::top);

你们需要将其定义为在参数的类别和(或)强烈类型之外使用的一种类型。 否则,它就被简单地定义为<条码>int,其查阅程序也必须公开:

 
class foo {
public:
    typedef enum DIRECTION {
        LEFT = 1,
        RIGHT,
        TOP,
        BOTTOM,
    };
    void SetDirection(foo::DIRECTION dir) {
        _direction = dir;
    }
    //...
protected:
    //...
private:
    foo::DIRECTION _direction;
    //...
};

int main() {
    //...
    foo bar;
    bar.SetDirection(foo::BOTTOM);
    //...
}

 




相关问题
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?

热门标签