Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Making Forward Declaration Class's enum Member Visible

Reply
Thread Tools

Making Forward Declaration Class's enum Member Visible

 
 
yancheng.cheok@gmail.com
Guest
Posts: n/a
 
      08-10-2006
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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      08-10-2006
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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      08-10-2006
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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-11-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
forward declaration & virtual member function return derived class kepeng@gmail.com C++ 6 07-03-2008 08:04 PM
member variable declaration and forward declarations John Ratliff C++ 2 08-26-2005 07:50 PM
Making enum visible to client ?? cmrchs@yahoo.com ASP .Net Web Services 1 06-16-2004 12:19 AM
Re-forward declaration of types which were already forward declared qazmlp C++ 1 02-15-2004 07:00 PM
GCC and forward declaration of enum Alexander Grigoriev C++ 11 09-14-2003 07:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57