Enumeration scope in namespace

Discussion in 'C++' started by Barzo, Mar 16, 2009.

  1. Barzo

    Barzo Guest

    Hi,

    I'm a little bit confusing about the enumerations scope into
    namespaces.
    I have this situation where MyLib.h is the interface header supplied
    to the customers.

    1. Is this a valid approch?
    2. The MyLib.cpp build fails...I have to specify the
    IMyBaseClass::MyEnum for each enumeraton?


    MyLib.h
    --------
    namespace MyNamespace
    {
    class IMyBaseClass
    {
    enum MyEnum
    {
    VAL_A,
    VAL_B
    }

    virtual MyEnum f() = 0;
    }

    class IMyClass : public IMyBaseClass
    {
    virtual void g( MyEnum value) = 0;
    }

    }


    MyLibImpl.h
    ------------
    #include "MyLib.h"

    namespace MyNamespace
    {
    class MyClass : public IMyClass
    {
    virtual MyEnum f();
    void g( MyEnum value );
    }
    }


    MyLibImpl.cpp
    -------------
    #include "MyLib.h"
    #include "MyLibImpl.h"

    using namespace MyNamespace;
    using MyNamespace::IMyBaseClass;

    MyEnum MyClass::f()
    {
    ...
    };

    void MyClass::g( MyEnum value )
    {
    ...
    };

    Thanks.
    Daniele.
     
    Barzo, Mar 16, 2009
    #1
    1. Advertising

  2. * Barzo alias Daniele:
    >
    > I'm a little bit confusing about the enumerations scope into
    > namespaces.
    > I have this situation where MyLib.h is the interface header supplied
    > to the customers.
    >
    > 1. Is this a valid approch?


    Probably, but see below.


    > 2. The MyLib.cpp build fails...I have to specify the
    > IMyBaseClass::MyEnum for each enumeraton?


    See the FAQ's advice on how to post a question about Code That Does Not Work.

    Generally the key idea is to be as specific as possible.


    > MyLib.h
    > --------
    > namespace MyNamespace
    > {
    > class IMyBaseClass
    > {
    > enum MyEnum
    > {
    > VAL_A,
    > VAL_B
    > }
    >
    > virtual MyEnum f() = 0;
    > }


    Missing semicolon. That means that this is code you have typed in instead of
    copied and pasted. And so it's *not* the code you're having problems with.

    And that means that we can only guess about whatever the problem is.


    > class IMyClass : public IMyBaseClass
    > {
    > virtual void g( MyEnum value) = 0;
    > }
    >
    > }
    >
    >
    > MyLibImpl.h
    > ------------
    > #include "MyLib.h"
    >
    > namespace MyNamespace
    > {
    > class MyClass : public IMyClass
    > {
    > virtual MyEnum f();
    > void g( MyEnum value );
    > }
    > }



    >
    >
    > MyLibImpl.cpp
    > -------------
    > #include "MyLib.h"
    > #include "MyLibImpl.h"
    >
    > using namespace MyNamespace;


    OK.


    > using MyNamespace::IMyBaseClass;


    This latter using declaration is superflous; you have already brought
    IMyBaseClass into the global namespace.


    > MyEnum MyClass::f()


    Should be e.g.

    MyClass::MyEnum MyClass::f()

    I don't like the C++ rules here, but anyway, you have to imagine a compiler with
    a very very narrow field of view and attention, scanning from left to right.
    While scanning the result type it doesn't yet know that this is in the context
    of a MyClass member function, and it doesn't look ahead to make sense of the
    type specification. So it needs to be force-fed that information.

    When the compiler gets to the formal arguments, however, it has understood that
    yes, we're dealing with MyClass.


    > {
    > ...
    > };
    >
    > void MyClass::g( MyEnum value )
    > {
    > ...
    > };
    >
    > Thanks.
    > Daniele.


    You're welcome.


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, Mar 16, 2009
    #2
    1. Advertising

  3. Barzo

    Barzo Guest

    On 16 Mar, 14:57, "Alf P. Steinbach" <> wrote:

    First, thanks for the support Alf!

    >
    > > 2. The MyLib.cpp build fails...I have to specify the
    > > IMyBaseClass::MyEnum for each enumeraton?

    >
    > See the FAQ's advice on how to post a question about Code That Does Not Work.
    > Generally the key idea is to be as specific as possible.
    >


    I'm sorry, in the future I'll pay more attention.


    > > MyLib.h
    > > --------
    > > namespace MyNamespace
    > > {
    > >   class IMyBaseClass
    > >   {
    > >     enum MyEnum
    > >     {
    > >       VAL_A,
    > >       VAL_B
    > >     }

    >
    > >     virtual MyEnum f() = 0;
    > >   }

    >
    > Missing semicolon. That means that this is code you have typed in instead of
    > copied and pasted. And so it's *not* the code you're having problems with..
    >
    > And that means that we can only guess about whatever the problem is.


    Yes, I wrote this code only to expain my problem.
    Another thing I should specify, sorry again.


    >
    > > MyEnum MyClass::f()

    >
    > Should be e.g.
    >
    >    MyClass::MyEnum MyClass::f()
    >
    > I don't like the C++ rules here, but anyway, you have to imagine a compiler with
    > a very very narrow field of view and attention, scanning from left to right.
    > While scanning the result type it doesn't yet know that this is in the context
    > of a MyClass member function, and it doesn't look ahead to make sense of the
    > type specification. So it needs to be force-fed that information.
    >
    > When the compiler gets to the formal arguments, however, it has understood that
    > yes, we're dealing with MyClass.
    >


    Oh, ok...
    I have thought that importing the namespace was sufficient for the
    compiler.
    I have modified my code and now seems to work!


    >
    > You're welcome.
    >
    > Cheers & hth.,
    >
    > - Alf


    Thanks again!
    Daniele.
     
    Barzo, Mar 16, 2009
    #3
    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. Paul Opal
    Replies:
    12
    Views:
    1,003
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    694
    Patricia Shanahan
    Sep 13, 2005
  3. Anonymous
    Replies:
    3
    Views:
    560
    Ron Natalie
    Aug 18, 2003
  4. Steven T. Hatton
    Replies:
    9
    Views:
    517
  5. puvit82
    Replies:
    4
    Views:
    779
    puvit82
    Feb 1, 2008
Loading...

Share This Page