Using enum in class

D

dev_15

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?
 
G

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

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?
 
D

dev_15

Thanks Erik. it just Windows programming they have all
their types and enums in Uppercase
 
R

robin

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
 
J

James Kanze

On 2007-10-22 11:24, dev_15 wrote:
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.
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.

[...]
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top