[comp.lang.c++] mutual dependence of types

atteson@eniac.seas.upenn.edu (Kevin Atteson ) (01/11/91)

Is there any way to do the following in C++ and if so how?

class A
{
	...
public:
	class B func1()
	{
		...
	}
};

class B
{
	...
public:
	class A func2()
	{
		...
	}
};

I don't want to return pointers to the classes.
I am a recent initiate to C++ and so I apologize if the question is obvious.
Please send responses directly to me and post them if you think it useful.

Thanks,

Kevin Atteson

glenn@huxley.huxley.bitstream.com (Glenn P. Parker) (01/11/91)

In article <35527@netnews.upenn.edu> atteson@eniac.seas.upenn.edu (Kevin Atteson ) writes:
> Is there any way to do the following in C++ and if so how?
> 
> [example of mutual class definition dependency elided]

Because of the mutual dependency in the definitions of classes A and B, you
can't *define* the functions func1 and func2 within their respective class
definitions.  They can still be inline, if that's important.  Just move the
function definitions after both of the necessary classes are defined.

class A {
public:
	class B func1();
};

class B {
public:
	class A func2();
};

inline B A::func1() { ... }
inline A B::func2() { ... }

--
Glenn P. Parker       glenn@bitstream.com       Bitstream, Inc.
                      uunet!huxley!glenn        215 First Street
                      BIX: parker               Cambridge, MA 02142-1270

jimad@microsoft.UUCP (Jim ADCOCK) (01/17/91)

In article <35527@netnews.upenn.edu> atteson@eniac.seas.upenn.edu (Kevin Atteson ) writes:
|Is there any way to do the following in C++ and if so how?
|
|class A
|{
|	...
|public:
|	class B func1()
|	{
|		...
|	}
|};
|
|class B
|{
|	...
|public:
|	class A func2()
|	{
|		...
|	}
|};
|
|I don't want to return pointers to the classes.
|I am a recent initiate to C++ and so I apologize if the question is obvious.
|Please send responses directly to me and post them if you think it useful.

No apologies necessary -- the number of C++ users continues to double
about every nine months.  Thus most C++ users are neophytes, and the 
few people who have been programming in C++ for a more than a year get to
play "expert". And thus, its important that these fundamental
questions continue to be asked and answered.  Consider:

extern "C"
{
	#include <stdio.h>
}

class A
{
public:
	A() { printf("making an A\t"); };
	class B func1();
};

class B
{
public:
	B() { printf("making a  B\t"); };
	class A func2();
};

B A::func1() { printf("\nIn A::func1 :"); return B(); }

A B::func2() { printf("\nIn B::func2 :"); return A(); }

main()
{
	A a;
	B b;
	a.func1();
	b.func2();
}