Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 10th, 2006, 08:15 AM
yancheng.cheok@gmail.com
Guest
 
Posts: n/a
Default Making Forward Declaration Class's enum Member Visible

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

  #2  
Old August 10th, 2006, 01:25 PM
mlimber
Guest
 
Posts: n/a
Default Re: Making Forward Declaration Class's enum Member Visible

yancheng.cheok@gmail.com wrote:
Quote:
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

  #3  
Old August 10th, 2006, 01:35 PM
mlimber
Guest
 
Posts: n/a
Default Re: Making Forward Declaration Class's enum Member Visible

mlimber wrote:
Quote:
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 };
Quote:
class cat
{
// ...
void MewAt( Canine::dog*, Canine::dog_enum );
};
}
Quote:
>
and in dog.h, you can do:
>
namespace Feline
{
enum cat_enum;
class cat;
}
>
namespace Canine
{
enum dog_enum { d1, d2 };
Quote:
class dog
{
// ...
void BarkAt( Feline::cat*, Feline::cat_enum );
};
}

Cheers! --M

  #4  
Old August 11th, 2006, 12:45 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Making Forward Declaration Class's enum Member Visible

yancheng.cheok@gmail.com wrote:
Quote:
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.
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles