473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Enums hierarchy and organization in C++

Hi!
I am trying to get several parameters in functions as enum variables.
Consider this:

namespace Layout
{
enum type
{
DOT, NEATO, FDP, TWOPI, CIRCO
};
}

A function signature would look like this:

void render(Layout::type L);

And a typical invocation could be:

G.render(Layout::NEATO).;

This looks really OOP and elegant. However, I am unsure whethere this
design pattern is the "de facto" standard one in modern C++. I wonder
how does one organize the enums to separate different enum items in a
nice way?
Jun 27 '08 #1
4 2473
zd***********@gmail.com wrote:
void render(Layout::type L);

And a typical invocation could be:

G.render(Layout::NEATO).;

This looks really OOP and elegant. However, I am unsure whethere this
design pattern is the "de facto" standard one in modern C++.
I wonder
how does one organize the enums to separate different enum items in a
nice way?
You've certainly nailed it. If C supported the concept of namespaces,
the declaration of an enum would place elements of the enumeration in
its namespace. Since it doesn't, doing it explicitly on your own is
perfectly legitimate and shouldn't surprise anyone.

With regard to whether it is a "de facto standard," I would go with no
it is not yet standard C++ style, but you should use namespaces anyway
and perhaps help that come about. It's quite a sensible style preference.

Other OOP languages that were designed without C compatibility in mind
(e.g. Java, C#) bring the enumerations into their own namespace as you
have done.

// C#
//
enum Layout {
DOT, NEATO, FDP, TWOPI, CIRCO
};

void Render(Layout type);

Render(Layout.NEATO);

--
Andrew Kerr
Jun 27 '08 #2
zd***********@gmail.com writes:
Hi!
I am trying to get several parameters in functions as enum variables.
Consider this:

namespace Layout
{
enum type
{
DOT, NEATO, FDP, TWOPI, CIRCO
};
}

A function signature would look like this:

void render(Layout::type L);

And a typical invocation could be:

G.render(Layout::NEATO).;

This looks really OOP and elegant. However, I am unsure whethere this
design pattern is the "de facto" standard one in modern C++. I wonder
how does one organize the enums to separate different enum items in a
nice way?
If I had my say, I would write:

namespace Layout{
namespace type{
enum type{
DOT, NEATO, FDP, TWOPI, CIRCO };}}

so we can add:

namespace Layout{
namespace type{
enum type{
DOT, NEATO, FDP, TWOPI, CIRCO };}
namespace form{
enum type{
DOT, CIRCLE, TRIANGLE, SQUARE, PENTAGON, POLYGON };}}

and no problem with Layout::type::DOT vs. Layout::form::DOT.

--
__Pascal Bourguignon__
Jun 27 '08 #3
On 5/30/2008 10:54 PM, zd***********@gmail.com wrote:
Hi!
I am trying to get several parameters in functions as enum variables.
Consider this:

namespace Layout
{
enum type
{
DOT, NEATO, FDP, TWOPI, CIRCO
};
}

A function signature would look like this:

void render(Layout::type L);

And a typical invocation could be:

G.render(Layout::NEATO).;

This looks really OOP and elegant. However, I am unsure whethere this
design pattern is the "de facto" standard one in modern C++. I wonder
how does one organize the enums to separate different enum items in a
nice way?
Wouldn't make a real OOP approach make more sense?
What about creating classes for DOT, NEATO, etc. ?

(Hhmm, where did I read "never switch on type codes"...Can't remember)
S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Jun 27 '08 #4
zd***********@gmail.com kirjutas:
Hi!
I am trying to get several parameters in functions as enum variables.
Consider this:

namespace Layout
{
enum type
{
DOT, NEATO, FDP, TWOPI, CIRCO
};
}

A function signature would look like this:

void render(Layout::type L);

And a typical invocation could be:

G.render(Layout::NEATO).;

This looks really OOP and elegant. However, I am unsure whethere this
design pattern is the "de facto" standard one in modern C++. I wonder
how does one organize the enums to separate different enum items in a
nice way?
Namespaces are fine. Another commonly used approach is to encapsulate the
enum into some class, which achieves visually the same effect. This
allows for finer access control and binds the enum more tightly with the
class. As std::ios_base is a class I would think this approach is more
"standard" than namespaces. It seems in Boost libraries both approaches
are used, whichever is more appropriate to the case at hand.

In case of namespaces, there should be more or less one namespace per
enum. If there are lots of other stuff in the same namespace, the client
code may want to apply "using namespace ...", thus destroying the enum
encapsulation. Of course, such enum namespaces would be nested ones
usually.

Another note: by many guidelines, all-caps identifiers are reserved for
preprocessor macros only. I would use some other style for enum
constants.

Best regards
Paavo
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bruce Hensley | last post by:
I don't know how to begin on a query (SELECT statement) to find all the tasks assigned to an arbitrary manager (say, staffID='JSmith') and her organization, that is, assigned to all her underlings,...
21
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new...
4
by: Kenneth Porter | last post by:
I'm trying to organize the XML for my system configuration. I have a System (top-level object) comprising a varying number of Components, each with its own configuration. I also have a number of...
4
by: Kenneth Porter | last post by:
I'm trying to organize the XML for my system configuration. I have a System (top-level object) comprising a varying number of Components, each with its own configuration. I also have a number of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.