Enums hierarchy and organization in C++

Discussion in 'C Programming' started by Guest, May 30, 2008.

  1. Guest

    Guest

    Joined:
    Feb 4, 2010
    Messages:
    13
    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?
     
    Guest, May 30, 2008
    #1
    1. Advertising

  2. Guest

    Andrew Kerr Guest

    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
     
    Andrew Kerr, May 30, 2008
    #2
    1. Advertising

  3. 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__
     
    Pascal J. Bourguignon, Jun 2, 2008
    #3
  4. Guest

    Stefan Naewe Guest

    On 5/30/2008 10:54 PM, 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
     
    Stefan Naewe, Jun 2, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amardeep Verma
    Replies:
    8
    Views:
    440
    Amir Pashazadeh
    Aug 26, 2003
  2. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,151
    Keith Thompson
    Jan 20, 2007
  3. Kenneth Porter

    Configuration hierarchy organization

    Kenneth Porter, Dec 27, 2007, in forum: XML
    Replies:
    6
    Views:
    426
    Kenneth Porter
    Jan 2, 2008
  4. Kenneth Porter

    Configuration hierarchy organization

    Kenneth Porter, Dec 27, 2007, in forum: C++
    Replies:
    6
    Views:
    335
    Kenneth Porter
    Jan 2, 2008
  5. kalensr
    Replies:
    0
    Views:
    96
    kalensr
    Mar 10, 2006
Loading...

Share This Page