[comp.sys.mac.programmer] Circular Referencing Think C Classes

brianj@witsend.cs.umd.edu (Brian Johnson) (09/06/90)

What do I need to do in order to let two classes "talk" to each other.
It took me awhile to figure out that this was my problem.  Is there
some sort of forward declaration for classes (class structs)
referenced in this way.

Here are four sample files.
Example Code:

#define	_H_Class1			/* Include this file only once */
#include "Class3.h"

struct Class1 : indirect {
	Class3	*three;
	void doit(void);
};

---------------------

#include "Class1.h"
#include <Stdio.h>

void	Class1::doit(void) {
	printf("I'm Class One.");
}

---------------------

#define	_H_Class3			/* Include this file only once */
#include "Class1.h"			

struct Class3 : indirect {
	Class1	*one;		
	void doit(void);
};

---------------------

#include "Class1.h"
#include <Stdio.h>

void	Class3::doit(void) {
	printf("I'm Class Three.");
}

Thanks,


--
Brian Johnson          Computer Science Department 
brianj@cs.umd.edu      University of Maryland  
(301) 405-2725         College Park, Md 20742     

dbw@cup.portal.com (Dale B Walker) (09/08/90)

You Write:

>What do I need to do in order to let two classes "talk" to each other.
>It took me awhile to figure out that this was my problem.  Is there
>some sort of forward declaration for classes (class structs)
>referenced in this way.

In your examples  you have two .h files that include each other.  
I encountered the same problem.
  
My solution was to have only one of the files include the other - 
and in the case of the second file, use the superclass of the desired 
class and cast it as necessary in the c code defining the methods.  

	Dale

Example:

/* Class1.h */

#define _H_Class1
#include <CObject.h>

struct Class1 : indirect {
	CObject		*three;
	
	void 		doit (void);
};
-------------------------------
/* Class3.h */

#define _H_Class3
#include <CObject.h>
#include "Class1.h"

struct Class3 : CObject {
	Class1		*one;
	
	void 		doit (void);
};
--------------------------------
/* Class1.c */

#include "Class1.h"
#include "Class3.h"

void
Class1::doit()
{
	((Class3 *) three)->doit();
}