Just an update, in case anyone comes across this post while looking for a
solution to a similar problem.
Someone has suggested I avoid the problem that I had by using integers instead
of enumerations. I like the type safety of enumerations, and I think that they
make code more readable. Of course, the dummy classes I have here are purely
academic, an illustration of the issue I encountered. The solution I'm using in
my real code is to have identical enumerations in both classes, so that I can
use the class's own enumeration in function prototypes, but then internally use
a case-switch to translate one enumeration to the other when I need to.
So, suppose in the Foo class I had a function like:
void Foo::setBBE(FooEnum fe)
{
switch(fe)
{
case A:
b->setBE(Bar::D);
break;
case B:
b->setBE(Bar::E);
break;
case C:
b->setBE(Bar::F);
break;
default:
cerr << "Invalid FooEnum, ignoring" << endl;
break;
}
}
I could also have used a map of FooEnum to BarEnum types, of course.
I couldn't have any Bar::BarEnum return types in Foo, of course, but I can work
with this.
I'm still interested in whether there's some way to use the innards of Bar in
prototypes for Foo, though, even if I don't really need to use it in my own
code, so if anyone can broaden my understanding of C++, it would be much
appreciated!
Thanks!
--- In [email protected], "muchos_churros" <blackwednes...@...> wrote:
>
> I've got a problem with a pair of classes that each refer to the other
> class. I've simplified things by writing a couple dummy classes, Foo and
> Bar. I've got them set up with a forward declaration for Bar, then Foo's
> definition, then Bar's definition:
>
> class Bar;
> class Foo{...};
> class Bar{...};
>
> Each defines an enumeration. Bar contains members of its own enumeration
> type (BarEnum) and Foo's enumeration type (FooEnum). Foo contains a
> member that's a pointer of type Bar and a member of type FooEnum. At
> this point, no problem. I've pasted my source from this stage below, it
> should compile.
>
> But I also want to be able to use BarEnum from within Foo, and I don't
> know how to make this work. I've got the lines that I used to try to do
> this commented out in the source pasted below. It's pretty clear to me
> why this can't possibly work, but is there some way to make something
> similar work? Perhaps a way to forward declare some of the innards of
> Bar?
>
> I'm starting to think this is a) totally illegal in C++, or b) just
> really bad programming.
>
> Any help would be much appreciated!
> Jessica
>
> File foo.h:
> #ifndef FOO_H
> #define FOO_H
> class Bar;
>
> class Foo
> {
> public:
> typedef enum {A,B,C} FooEnum;
> Foo();
> FooEnum getFE() const;
> FooEnum getBFE() const;
> //Bar::BarEnum getBBE() const;
> private:
> Bar *b;
> FooEnum fe;
> };
>
> class Bar
> {
> public:
> typedef enum {D = 10, E, F} BarEnum;
> Bar();
> BarEnum getBE() const;
> Foo::FooEnum getFE() const;
> private:
> Foo::FooEnum fe;
> BarEnum be;
> };
>
> #endif
>
> File foo.cpp:
> #include <iostream>
> using namespace std;
> #include "foo.h"
>
> Foo::Foo()
> {
> fe = A;
> b = new Bar();
> }
>
> Foo::FooEnum Foo::getFE() const
> {
> return fe;
> }
>
> Foo::FooEnum Foo::getBFE() const
> {
> return b->getFE();
> }
>
> /*Bar::BarEnum Foo::getBBE() const
> {
> return b->getBE();
> }*/
>
> Bar::Bar()
> {
> fe = Foo::A;
> be = D;
> }
>
> Bar::BarEnum Bar::getBE() const
> {
> return be;
> }
>
> Foo::FooEnum Bar::getFE() const
> {
> return fe;
> }
>
> int main()
> {
> Foo f;
> Bar b;
> cout << "f's FooEnum val: " << f.getFE() << endl;
> cout << "f's b member FooEnum val: " << f.getBFE() << endl;
> //cout << " and BarEnum val: " << f.getBBE() << endl;
> cout << "b's FooEnum val: " << b.getFE();
> cout << " and BarEnum val: " << b.getBE() << endl;
>
>
> }
>
>
> [Non-text portions of this message have been removed]
>