Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using enum in class

Reply
Thread Tools

Using enum in class

 
 
dev_15
Guest
Posts: n/a
 
      10-22-2007
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?

 
Reply With Quote
 
 
 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      10-22-2007
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:ISPLAYMODE 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
 
Reply With Quote
 
 
 
 
dev_15
Guest
Posts: n/a
 
      10-22-2007
Thanks Erik. it just Windows programming they have all
their types and enums in Uppercase

 
Reply With Quote
 
robin
Guest
Posts: n/a
 
      10-22-2007
On Oct 22, 5:24 pm, dev_15 <naumansulai...@googlemail.com> 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:ISPLAYMODE CDisplayUtil::GetDisplayMode(){...}
2).
if(nHeight > nWidth)
return CDisplayUtil:ORTRAIT;
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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-23-2007
On Oct 22, 11:43 am, Erik Wikström <Erik-wikst...@telia.com> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"enum" vs. "enum class" Ansel C++ 16 08-27-2012 10:34 AM
Difference between enum class and enum struct Brian C++ 4 02-27-2010 04:03 PM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments