473,799 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2500
zd***********@g mail.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.N EATO);

--
Andrew Kerr
Jun 27 '08 #2
zd***********@g mail.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::D OT vs. Layout::form::D OT.

--
__Pascal Bourguignon__
Jun 27 '08 #3
On 5/30/2008 10:54 PM, zd***********@g mail.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***********@g mail.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
11023
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, and their underlings, and .... For that matter, I don't even know how to find everyone in her organization (at all levels). - All individuals have only one manager - Tasks are assigned to individuals - A manager at any level may have direct...
21
1984
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 ("Pear"); Basket.Items.Add(item); //------
4
1425
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 classes representing stateful bundles of related methods. The latter classes need per-component configuration. Should that configuration be grouped with each Component's configuration, or should it be stored with the configuration for each bundle...
4
1898
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 classes representing stateful bundles of related methods. The latter classes need per-component configuration. Should that configuration be grouped with each Component's configuration, or should it be stored with the configuration for each bundle...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10470
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.