Topic: nested enums and typedefs


Author: jeffturneresq@my-deja.com
Date: 2000/04/15
Raw View
In article <38EB20EF.A4BA0E2F@myview.de>,
  Robert Klemme <robert.klemme@myview.de> wrote:
>
>
> Scott Blachowicz schrieb:
> >
> > >can anybody provide me with some enlightenment about the reasoning
> > >behind this: consider the following code
> > >
> > >struct A {
> > >  enum State { stop, start, running };
> > >};
> > >
> > >struct B {
> > >  typedef A::State AnotherState;
> > >};
> >
> > I suppose that in order to get your desired effect, you would have
to
> > inherit from that A class:
> >
> >     struct A {
> >         enum State { stop, start, running };
> >     };
> >     struct B : public A {
> >     };
>
> this is not a solution where those two classes belong to different
> layers of the code.  the relationship between them is not a 'is-a' but
a
> 'uses' relationship.
>
> regards
>
>  robert

if it's a uses relationship, maybe declaring the function that uses it
as a friend might work.  Just off the top of my head, no reference at
hand.

--Jeff Turner
using std::disclaimer


Sent via Deja.com http://www.deja.com/
Before you buy.



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Scott Blachowicz <scott@sabmail.rresearch.com>
Date: 2000/04/13
Raw View
> > I suppose that in order to get your desired effect, you would have
to
> > inherit from that A class:
> >
> >     struct A {
> >         enum State { stop, start, running };
> >     };
> >     struct B : public A {
> >     };
>
> this is not a solution where those two classes belong to different
> layers of the code.  the relationship between them is not a 'is-a' but
a
> 'uses' relationship.

Yes, but...if your enum-defining struct contains ONLY the enum, then you
could inherit from it in all of the other classes that need that enum.
So, something like this:

    struct WrapEnumState {
        enum State { stop, start, running };
    };
    struct A : public WrapEnumState {
        // ...etc...
    };
    struct DerivedB : public BaseB, public WrapEnumState {
        // ..etc...
        A a;
    };

So, it's just using the inheritance as a way to import the enum values
into the class.  No - I've never done this before...just playing
guessing
games here :-).

--
Scott.Blachowicz@seaslug.org

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Robert Klemme <robert.klemme@myview.de>
Date: 2000/04/11
Raw View


Scott Blachowicz schrieb:
>
> >can anybody provide me with some enlightenment about the reasoning
> >behind this: consider the following code
> >
> >struct A {
> >  enum State { stop, start, running };
> >};
> >
> >struct B {
> >  typedef A::State AnotherState;
> >};
>
> I suppose that in order to get your desired effect, you would have to
> inherit from that A class:
>
>     struct A {
>         enum State { stop, start, running };
>     };
>     struct B : public A {
>     };

this is not a solution where those two classes belong to different
layers of the code.  the relationship between them is not a 'is-a' but a
'uses' relationship.

regards

 robert

--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra_e 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/04/03
Raw View
In article <38E32E8B.50E9F40@myview.de>, Robert Klemme
<robert.klemme@myview.de> writes
>hm...  how would you suggest to cope with the following situation.  in a
>package (for this example a set of classes) there are several classes,
>some which are intended to be the public interface and others not.  if
>in an inner class an enum is defined which should be used in the
>interface of a public class then it would be the easiest way to just use
>the typedef construct to draw it into the scope of the public class.
>otherwise i would have to define an enum in the public class and a
>conversion function.  this leads to a situation where there are two
>enums and thus redundant information.  would you suggest putting the
>enum in global scope or into a namespace?

Once you make an enum public it can be used anywhere so there is no
great advantage in continuing the old mechanism of encapsulating such
enums in classes, just put them in a suitable namespace.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: David R Tribble <david@tribble.com>
Date: 2000/04/04
Raw View
Scott Blachowicz wrote:
>
>> can anybody provide me with some enlightenment about the reasoning
>> behind this: consider the following code
>>
>> struct A {
>>   enum State { stop, start, running };
>> };
>>
>> struct B {
>>   typedef A::State AnotherState;
>> };
>
> I suppose that in order to get your desired effect, you would have to
> inherit from that A class:
>
>     struct A {
>         enum State { stop, start, running };
>     };
>     struct B : public A {
>     };
>
> then you should be able to say B::start, I think.  There does seem to
> be some nice things that could've been done with enums...being able
> to suck the values into a different scope with a 'using' of some sort,
> being able to augment them (like subclassing), having some mechanism
> to get automatic stringifications of them (e.g. built in 'const char
> *' converters), ...
>
> Oh well...

Yes, if enums introduced a separate scope for their names, C++ could
have had allowed some nice things for enums.  But alas.

The example I like to use for this situation is a base class containing
a set of enum codes (e.g., error codes or perhaps bitmask values),
and a derived class that adds more constants to the enum list.
There is no nice way of doing it in C++.

There are a couple of not-so-nice ways of doing it, though:

    // I
    struct B: public A
    {
        enum AnotherState
        {
            paused = A::running+1,     // Additional state codes
            resume
        };
        ...
    };

    // II
    struct B
    {
        enum AnotherState
        {
            stop =      A::stop,
            start =     A::start,
            running =   A::running,
            paused,     // Additional state codes
            resume
        };
        ...
    };

--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Scott Blachowicz <Scott.Blachowicz@seaslug.org>
Date: 2000/04/04
Raw View
David R Tribble <david@tribble.com> writes:

> There are a couple of not-so-nice ways of doing it, though:
>
>     // I
>     struct B: public A
>     {
>         enum AnotherState
>         {
>             paused = A::running+1,     // Additional state codes
>             resume
>         };
>         ...
>     };

Yeah...I've done variants on that before to add "command opcodes" to a set
of "command" objects like this sort of stuff:

    struct BaseCommand {
        enum CommandTypes {
            ...,
            BaseNextCommand    // Where new cmd types start.
        }
    };

    struct DerivedCommand : public BaseCommand {
        enum CommandTypes {
            FirstNewCommand = BaseCommand::BaseNextCommand,
            SecondNewCommand,
            DerivedNextCommand
        };
    };

Or something like that...kind of a pain.

--
Scott

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Robert Klemme <robert.klemme@myview.de>
Date: 2000/03/31
Raw View


Francis Glassborow schrieb:
> No, you misunderstand.  There is no nested type in B, just a local name
> for a nested type from A.

well, ok, you seem to be right on that one.

> More to the point, why are you wishing to use a nested type of struct A
> in the scope of struct B?  I think your problems are an artefact of a
> poor design.

hm...  how would you suggest to cope with the following situation.  in a
package (for this example a set of classes) there are several classes,
some which are intended to be the public interface and others not.  if
in an inner class an enum is defined which should be used in the
interface of a public class then it would be the easiest way to just use
the typedef construct to draw it into the scope of the public class.
otherwise i would have to define an enum in the public class and a
conversion function.  this leads to a situation where there are two
enums and thus redundant information.  would you suggest putting the
enum in global scope or into a namespace?

regards

 robert

--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra   e 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Scott Blachowicz <Scott.Blachowicz@seaslug.org>
Date: 2000/03/31
Raw View
>can anybody provide me with some enlightenment about the reasoning
>behind this: consider the following code
>
>struct A {
>  enum State { stop, start, running };
>};
>
>struct B {
>  typedef A::State AnotherState;
>};

I suppose that in order to get your desired effect, you would have to
inherit from that A class:

    struct A {
        enum State { stop, start, running };
    };
    struct B : public A {
    };

then you should be able to say B::start, I think.  There does seem to be
some nice things that could've been done with enums...being able to suck
the values into a different scope with a 'using' of some sort, being able
to augment them (like subclassing), having some mechanism to get automatic
stringifications of them (e.g. built in 'const char *' converters), ...

Oh well...

--
Scott.Blachowicz@seaslug.org

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/31
Raw View


Robert Klemme wrote:

> struct A {
>   enum State { stop, start, running };
> };
>
> struct B {
>   typedef A::State AnotherState;
> };
>
> why then is it that i cannot use B::stop?

Because there is no "stop" in B's scope.  The typedef just makes an alias
for the type, it doesn't redeclare the enum in the new scope.



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Robert Klemme <robert.klemme@myview.de>
Date: 2000/03/28
Raw View

hi all,

can anybody provide me with some enlightenment about the reasoning
behind this: consider the following code

struct A {
  enum State { stop, start, running };
};

struct B {
  typedef A::State AnotherState;
};

why then is it that i cannot use B::stop?  now we have the strange
situation, that there is a nested type in struct B but i can just use
values from scope A.  is this just a compiler oddity? (microsoft vc++
6.0)  or is there a serious reason for this that i oversaw?  thank you!

regards

 robert

--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra   e 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/29
Raw View
In article <38E0AF3D.4B4B2F76@myview.de>, Robert Klemme
<robert.klemme@myview.de> writes
>
>
>hi all,
>
>can anybody provide me with some enlightenment about the reasoning
>behind this: consider the following code
>
>struct A {
>  enum State { stop, start, running };
>};
>
>struct B {
>  typedef A::State AnotherState;

That line introduces an alternative name for the type State.  This
alternative name is confined to the scope of B.  However that says
nothing about the three enumerated values.
>};


>
>why then is it that i cannot use B::stop?  now we have the strange
>situation, that there is a nested type in struct B

No, you misunderstand.  There is no nested type in B, just a local name
for a nested type from A. This just how all names belonging to types
work (try a nested union instead of a nested enum).  The special feature
of an enum type is that there is no scope introduced by an enum.


>but i can just use
>values from scope A.  is this just a compiler oddity? (microsoft vc++
>6.0)  or is there a serious reason for this that i oversaw?  thank you!
>

More to the point, why are you wishing to use a nested type of struct A
in the scope of struct B?  I think your problems are an artefact of a
poor design.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/03/29
Raw View

Robert Klemme <robert.klemme@myview.de> wrote in message
news:38E0AF3D.4B4B2F76@myview.de...
>
>
> hi all,
>
> can anybody provide me with some enlightenment about the reasoning
> behind this: consider the following code
>
> struct A {
>   enum State { stop, start, running };
> };
>
> struct B {
>   typedef A::State AnotherState;
// You may write now:
  void f() {
    A::State as = A::stop;
    // and
    AnotherState as2 = A::stop; // May be considered as a little bit more
clear. Just imagin: A::A1::A2::...::AN::State as2
            // But you should have another typedef to use stop, start and
running
            //  typedef A::A1::A2::...::AN MyA
            //  and use AnotherState as2 = MyA::stop;  // I mean <typedef
A::A1::A2::...::AN::State AnotherState> here
            // instead of A::A1::A2::...::AN::State as2 =
A::A1::A2::...::AN::stop;
  }

> };
>
> why then is it that i cannot use B::stop?  now we have the strange
It is because 'stop' is not a member of 'B'.

> situation, that there is a nested type in struct B but i can just use
> values from scope A.  is this just a compiler oddity? (microsoft vc++
The oddity of MS compiler (and not MS only) is that A::State::stop is
allowed. A::State is not a namespace indeed. It is surprising but MS does
not allow AnotherState as2 = AnotherState::stop in the example above. That
may be considered as an inconsequence in this compiler errors.

With regards,
Michael Kochetkov.

> 6.0)  or is there a serious reason for this that i oversaw?  thank you!
>
> regards
>
> robert
>
> --
> Robert Klemme
> Software Engineer
> -------------------------------------------------------------
> myview technologies GmbH & Co. KG
> Riemekestra   e 160 ~ D-33106 Paderborn ~ Germany
> E-Mail: mailto:robert.klemme@myview.de
> Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
> -------------------------------------------------------------
>
>
>
>       [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
>       [ about comp.lang.c++.moderated. First time posters: do this! ]
>
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>
>



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: James Kuyper <kuyper@wizard.net>
Date: 2000/03/30
Raw View
Robert Klemme wrote:
>
> hi all,
>
> can anybody provide me with some enlightenment about the reasoning
> behind this: consider the following code
>
> struct A {
>   enum State { stop, start, running };
> };
>
> struct B {
>   typedef A::State AnotherState;
> };
>
> why then is it that i cannot use B::stop?  now we have the strange

Because you haven't defined anything with that name? There is an
identifier named A::stop, and it can be used in connection with
B::AnotherState.



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]