Using enum in class

Discussion in 'C++' started by dev_15, Oct 22, 2007.

  1. dev_15

    dev_15 Guest

    Hi, I have this problem that i want to use an enum as a return value
    of a private method in my class, but compiler won't let me use as such

    Code here:

    class CDisplayUtil
    {
    public:
    enum DISPLAYMODE
    {
    LANDSCAPE = -1,
    SQUARE = 0,
    PORTRAIT = 1
    };

    public:
    CDisplayUtil();
    public:

    virtual ~CDisplayUtil(void);
    private:
    DISPLAYMODE GetDisplayMode();
    };



    CDisplayUtil::CDisplayUtil()
    {
    }

    CDisplayUtil::~CDisplayUtil()
    {
    }

    DISPLAYMODE CDisplayUtil::GetDisplayMode()
    {
    INT nWidth = GetSystemMetrics(SM_CXSCREEN);
    INT nHeight = GetSystemMetrics(SM_CYSCREEN);

    if(nHeight > nWidth)
    return PORTRAIT;

    if(nHeight < nWidth)
    return LANDSCAPE;

    return SQUARE;
    }

    What's wrong?
    dev_15, Oct 22, 2007
    #1
    1. Advertising

  2. On 2007-10-22 11:24, dev_15 wrote:
    > Hi, I have this problem that i want to use an enum as a return value
    > of a private method in my class, but compiler won't let me use as such
    >
    > Code here:
    >
    > class CDisplayUtil
    > {
    > public:
    > enum DISPLAYMODE


    All uppercase names are usually reserved for macros. An enumeration is a
    type just like a class, so perhaps EDisplayMode would be a more suitable
    name.

    > {
    > LANDSCAPE = -1,
    > SQUARE = 0,
    > PORTRAIT = 1


    Again, Landscape, Square, and Portrait might be better names.

    > };
    >
    > public:


    You do not need to put public before every member, only when you want to
    change visibility.

    > CDisplayUtil();
    > public:
    >
    > virtual ~CDisplayUtil(void);
    > private:
    > DISPLAYMODE GetDisplayMode();
    > };
    >
    >
    >
    > CDisplayUtil::CDisplayUtil()
    > {
    > }
    >
    > CDisplayUtil::~CDisplayUtil()
    > {
    > }
    >
    > DISPLAYMODE CDisplayUtil::GetDisplayMode()


    CDisplayUtil::DISPLAYMODE CDisplayUtil::GetDisplayMode()

    The enumeration is part of the class.

    > {
    > INT nWidth = GetSystemMetrics(SM_CXSCREEN);


    What is wrong with the old honest int?

    > INT nHeight = GetSystemMetrics(SM_CYSCREEN);
    >
    > if(nHeight > nWidth)
    > return PORTRAIT;
    >
    > if(nHeight < nWidth)
    > return LANDSCAPE;
    >
    > return SQUARE;
    > }
    >
    > What's wrong?


    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Oct 22, 2007
    #2
    1. Advertising

  3. dev_15

    dev_15 Guest

    Thanks Erik. it just Windows programming they have all
    their types and enums in Uppercase
    dev_15, Oct 22, 2007
    #3
  4. dev_15

    robin Guest

    On Oct 22, 5:24 pm, dev_15 <> wrote:
    > Hi, I have this problem that i want to use an enum as a return value
    > of a private method in my class, but compiler won't let me use as such
    >
    > Code here:
    >
    > class CDisplayUtil
    > {
    > public:
    > enum DISPLAYMODE
    > {
    > LANDSCAPE = -1,
    > SQUARE = 0,
    > PORTRAIT = 1
    > };
    >
    > public:
    > CDisplayUtil();
    > public:
    >
    > virtual ~CDisplayUtil(void);
    > private:
    > DISPLAYMODE GetDisplayMode();
    >
    > };
    >
    > CDisplayUtil::CDisplayUtil()
    > {
    >
    > }
    >
    > CDisplayUtil::~CDisplayUtil()
    > {
    >
    > }
    >
    > DISPLAYMODE CDisplayUtil::GetDisplayMode()
    > {
    > INT nWidth = GetSystemMetrics(SM_CXSCREEN);
    > INT nHeight = GetSystemMetrics(SM_CYSCREEN);
    >
    > if(nHeight > nWidth)
    > return PORTRAIT;
    >
    > if(nHeight < nWidth)
    > return LANDSCAPE;
    >
    > return SQUARE;
    >
    > }
    >
    > What's wrong?


    Once you define an enum inside a class, this class also defines a
    namespace meanwhile. Therefore, here if you want to use DISPLAYMODE
    enum, you should specify its namespace, that is, the class name in
    which this enum is defined.

    The correct lines of code should be:

    1). CDisplayUtil::DISPLAYMODE CDisplayUtil::GetDisplayMode(){...}
    2).
    if(nHeight > nWidth)
    return CDisplayUtil::pORTRAIT;
    if(nHeight < nWidth)
    return CDisplayUtil::LANDSCAPE;
    return CDisplayUtil::SQUARE;

    The function declaration of
    private:
    DISPLAYMODE GetDisplayMode();
    does not need to go with the class name because this function
    declaration is still inside the class CDisplayUtil scope, and the
    definition of DISPLAYMODE is visible. But when you define the member
    function after the closing brace('}') of class CDisplayUtil, which
    means you go out of the scope of CDisplayUtil and into the global
    scope now, the DISPLAYMODE becomes invisible, and you need to give the
    full qualified name to help compiler find it.

    Regards,
    -robin
    robin, Oct 22, 2007
    #4
  5. dev_15

    James Kanze Guest

    On Oct 22, 11:43 am, Erik Wikström <> wrote:
    > On 2007-10-22 11:24, dev_15 wrote:


    > > Hi, I have this problem that i want to use an enum as a
    > > return value of a private method in my class, but compiler
    > > won't let me use as such


    > > Code here:


    > > class CDisplayUtil
    > > {
    > > public:
    > > enum DISPLAYMODE


    > All uppercase names are usually reserved for macros. An
    > enumeration is a type just like a class, so perhaps
    > EDisplayMode would be a more suitable name.


    And perhaps just DisplayMode would be even more suitable;
    DisplayMode is scoped, so there's no need for any special naming
    convention (except maybe to distinguish the fact that it is a
    type, and not something else).

    In the same vein, Microsoft uses the C prefix for MFC, so user
    defined classes should avoid it, using something else, or in
    modern C++, putting their classes in a namespace, and not using
    any prefix.

    > > {
    > > LANDSCAPE = -1,
    > > SQUARE = 0,
    > > PORTRAIT = 1


    > Again, Landscape, Square, and Portrait might be better names.


    Or landscape, square and portrait.

    Naming conventions vary, but generally, I find it best to
    distinguish: macros (all caps, as you say), types and everything
    else. One of the conventions for the latter two is that types
    begin with a capital, other things with a small letter.

    An even better convention might be to name things clearly enough
    that whether something is a type or not is clear from the
    semantics of its name. A type is an unqualified noun, a value a
    qualified noun, and a function a verb. The context here is such
    that simply Mode or mode would be an adequate name for the
    enum---what other mode would there be in a class called
    DisplayUtil. And the enum values would then be landscapeMode
    (or landscape_mode), etc. In practice, however, I find
    distinguishing type by some meta convention useful anyway.

    [...]
    > > {
    > > INT nWidth = GetSystemMetrics(SM_CXSCREEN);


    > What is wrong with the old honest int?


    Anyone who knows C++ will know what it means immediately. INT
    forces the reader to look up in the documentation what it really
    is.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Oct 23, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    540
  2. Jerminia
    Replies:
    3
    Views:
    624
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    463
    Rolf Magnus
    Nov 13, 2003
  4. Brian
    Replies:
    4
    Views:
    2,636
    Brian
    Feb 27, 2010
  5. Ansel

    "enum" vs. "enum class"

    Ansel, Aug 26, 2012, in forum: C++
    Replies:
    16
    Views:
    746
    Ansel
    Aug 27, 2012
Loading...

Share This Page