Skip to main content
GameDev.net gamedev.net
🔒 Locked

Class dependency/declaration problem

Started by magixts Dec 30, 2008 at 11:48 PM 3 replies 700+ views
Original Post
magixts
magixts
Hi! I've run into a problem with two classes that both depend on each other to be defined. Class A can create objects of class B, class B takes a reference to an object of class A in its constructor. Is there any way to declare the classes so that they are aware of each others existence or some other way around this problem? I've attached some sample code below to illustrate my problem:

//Declarations (that do not work)
class A;
class B;

//Definition of class A
class A {
protected:
	~A() {
		if(m_b != NULL) {
			m_b->Release();
			m_b = NULL;
		}
	}
	
public:
	A() : m_iRefs(0), m_b(NULL) { };
	void CreateB(B**_b) {
		*_b = new B(*this);
		(*_b)->AddRef();
		this->AddRef();

		if(m_b != NULL) {
			m_b->Release();
			m_b = NULL;
		}

		m_b = *_b;
		m_b->AddRef();
	};
	void AddRef() { m_iRefs++; }
	void Release() {
		if(--m_iRefs <= 0) {
			delete this;
		}
	};
	
private:
	unsigned int m_iRefs;
	B *m_b;
}

//Definition of class B
class B {
protected:
	~B() {
		m_a.Release();
	}
	
public:
	B(A &_a) : m_iRefs(0), m_a(_a) { };
	void AddRef() { m_iRefs++; }
	void Release() {
		if(--m_iRefs <= 0) {
			delete this;
		}
	};

private:
	unsigned int m_iRefs;
	A &m_a;
}

int main() {
	A *a = new A();
	B *b = NULL;
	a->CreateB(&b);

	b->Release();
	a->Release();
}


Is anyone able to offer some help? Thanks in advance!
Xai
Xai
1 of the 2 can only have a POINTER to the other ... at most ...

so if A makes's B's ... then a "could" contain B's or lists of B's ... but then B would only be able to have a pointer to "A", not an object or reference.
Bahdom
Bahdom
Although circular dependancies are "doable" they are usually the result of bad design (at least in my experience). Try to find some other way to implement this.
MaulingMonkey
MaulingMonkey
Separate the function declarations and definitions. The problem isn't that your classes can't mutually know about the existence of each other, it's that they can't both know the details (members, functions, etc) about each other at the same time before definition.

However, since only your class's functions need to know these details, simply moving the definition of the functions until after the classes have been defined will solve your problem.
magixts
magixts
Quote:
Original post by MaulingMonkey
Separate the function declarations and definitions. The problem isn't that your classes can't mutually know about the existence of each other, it's that they can't both know the details (members, functions, etc) about each other at the same time before definition.

However, since only your class's functions need to know these details, simply moving the definition of the functions until after the classes have been defined will solve your problem.


Thank you very much! :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.