Making Forward Declaration Class's enum Member Visible

Discussion in 'C++' started by yancheng.cheok@gmail.com, Aug 10, 2006.

  1. Guest

    hello all,

    how can i make, a forward declaration class's enum member, being
    visible by another class?

    consider the following case,

    ----------------------------
    dog.h
    ----------------------------
    #ifndef DOG_H
    #define DOG_H

    // class forward declaration.
    class cat;

    class dog
    {
    public:
    enum dog_enum
    {
    d0, d1, d2
    };

    void speak(cat *c);
    };

    #endif

    ----------------------------
    cat.h
    ----------------------------
    #ifndef CAT_H
    #define CAT_H

    #include "dog.h"

    class cat
    {
    public:
    void speak(dog *d, dog::dog_enum e);
    };

    #endif

    The above cat and dog just work fine. Now, let me create an enum type
    for cat too.

    ----------------------------
    dog.h
    ----------------------------
    #ifndef DOG_H
    #define DOG_H

    // class forward declaration.
    class cat;

    class dog
    {
    public:
    enum dog_enum
    {
    d0, d1, d2
    };

    // OPPS! HOW DO WE FORWARD DECLARE ENUM???
    void speak(cat *c, cat::cat_enum e);
    };

    #endif

    ----------------------------
    cat.h
    ----------------------------
    #ifndef CAT_H
    #define CAT_H

    #include "dog.h"

    class cat
    {
    public:
    enum cat_enum
    {
    c0, c1, c2
    };

    void speak(dog *d, dog::dog_enum e);
    };

    #endif

    My question is, how can "dog" see the cat_enum, which is re-inside cat?
    I was understand that forward declaration for enum is not allowed in
    c++.

    Is there any workaround for this?

    Thank you very much
    , Aug 10, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > how can i make, a forward declaration class's enum member, being
    > visible by another class?
    >
    > consider the following case,
    >
    > ----------------------------
    > dog.h
    > ----------------------------
    > #ifndef DOG_H
    > #define DOG_H
    >
    > // class forward declaration.
    > class cat;
    >
    > class dog
    > {
    > public:
    > enum dog_enum
    > {
    > d0, d1, d2
    > };
    >
    > void speak(cat *c);
    > };
    >
    > #endif
    >
    > ----------------------------
    > cat.h
    > ----------------------------
    > #ifndef CAT_H
    > #define CAT_H
    >
    > #include "dog.h"
    >
    > class cat
    > {
    > public:
    > void speak(dog *d, dog::dog_enum e);
    > };
    >
    > #endif
    >
    > The above cat and dog just work fine. Now, let me create an enum type
    > for cat too.
    >
    > ----------------------------
    > dog.h
    > ----------------------------
    > #ifndef DOG_H
    > #define DOG_H
    >
    > // class forward declaration.
    > class cat;
    >
    > class dog
    > {
    > public:
    > enum dog_enum
    > {
    > d0, d1, d2
    > };
    >
    > // OPPS! HOW DO WE FORWARD DECLARE ENUM???
    > void speak(cat *c, cat::cat_enum e);
    > };
    >
    > #endif
    >
    > ----------------------------
    > cat.h
    > ----------------------------
    > #ifndef CAT_H
    > #define CAT_H
    >
    > #include "dog.h"
    >
    > class cat
    > {
    > public:
    > enum cat_enum
    > {
    > c0, c1, c2
    > };
    >
    > void speak(dog *d, dog::dog_enum e);
    > };
    >
    > #endif
    >
    > My question is, how can "dog" see the cat_enum, which is re-inside cat?
    > I was understand that forward declaration for enum is not allowed in
    > c++.
    >
    > Is there any workaround for this?


    Yes: pull the enum outside the class, putting it in the same namespace
    as its associated class. Then in cat.h, you can do:

    namespace Canine
    {
    enum dog_enum;
    class dog;
    }

    class cat
    {
    // ...
    void MewAt( Canine::dog*, Canine::dog_enum );
    };

    and in dog.h, you can do:

    namespace Feline
    {
    enum cat_enum;
    class cat;
    }

    class dog
    {
    // ...
    void BarkAt( Feline::cat*, Feline::cat_enum );
    };

    Cheers! --M
    mlimber, Aug 10, 2006
    #2
    1. Advertising

  3. mlimber Guest

    mlimber wrote:
    > Yes: pull the enum outside the class, putting it in the same namespace
    > as its associated class. Then in cat.h, you can do:
    >
    > namespace Canine
    > {
    > enum dog_enum;
    > class dog;
    > }
    >


    namespace Feline // Forgot this... sorry
    {
    enum cat_enum { c1, c2 };
    > class cat
    > {
    > // ...
    > void MewAt( Canine::dog*, Canine::dog_enum );
    > };


    }

    >
    > and in dog.h, you can do:
    >
    > namespace Feline
    > {
    > enum cat_enum;
    > class cat;
    > }
    >


    namespace Canine
    {
    enum dog_enum { d1, d2 };
    > class dog
    > {
    > // ...
    > void BarkAt( Feline::cat*, Feline::cat_enum );
    > };


    }

    Cheers! --M
    mlimber, Aug 10, 2006
    #3
  4. Ron Natalie Guest

    wrote:

    > My question is, how can "dog" see the cat_enum, which is re-inside cat?
    > I was understand that forward declaration for enum is not allowed in
    > c++.
    >
    > Is there any workaround for this?
    >

    Declare the enums separately from the classes. Use a namespace
    if you like.
    Ron Natalie, Aug 11, 2006
    #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. Alexander Grigoriev

    GCC and forward declaration of enum

    Alexander Grigoriev, Sep 12, 2003, in forum: C++
    Replies:
    11
    Views:
    1,284
    Gianni Mariani
    Sep 14, 2003
  2. qazmlp
    Replies:
    1
    Views:
    593
    Jonathan Turkanis
    Feb 15, 2004
  3. John Ratliff
    Replies:
    2
    Views:
    317
    John Ratliff
    Aug 26, 2005
  4. Replies:
    6
    Views:
    523
  5. Making enum visible to client ??

    , Jun 15, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    270
    Dino Chiesa [Microsoft]
    Jun 16, 2004
Loading...

Share This Page