Enums hierarchy and organization in C++

Joined
Feb 4, 2010
Messages
28
Reaction score
0
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?
 
A

Andrew Kerr

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);
 
P

Pascal J. Bourguignon

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.
 
S

Stefan Naewe

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.
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top