2 classes that are dependant on each other

Started by
15 comments, last by b1gjo3 15 years, 11 months ago
how can i have something like this

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

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

Advertisement
You can have a forward declaration.
But in your case, how could you ever create either A or B, when the constructors of both need an object of the other type?
As is, that is impossible. In order to create an instance of A, one needs a B instance, and to make a B an A needs to exist.

Without the actual context (which is typically important, most such problems can be designed around) it is hard to give any concrete advice. For example, instead of B having an A as a member, it might be possible to pass an A instance to member functions of B that require an A. Or, it might be possible to use a reseatable reference, like a (smart) pointer, on one side of the relationship.
what if only one of the classes above needed to be passed in the other class through the constructor.

like this
class A{public:	A();	~A();	SomeFunc()	{		//create instance of class B and do stuff	}};class B{public:	B(A);	~B();};


i think i still have the same problem
Quote:Original post by b1gjo3
i think i still have the same problem


No, A can use the this pointer to create B:

void A::SomeFunc() {    B(*this);    //...}
just one extra point - unless you want B to have a copy of A then

B(const A&) or B(A&) would make more sense. And then you could call

void A::SomeFunc(){     B(this);     //...}
Quote:Original post by Gage64
Quote:Original post by b1gjo3
i think i still have the same problem


No, A can use the this pointer to create B:

*** Source Snippet Removed ***


when i do this, i get an error saying the class has no constructors
Post the actual errors.

If you have changed the code quite a bit, post the updated code.

This works for me:
class A{public:    A(int value) : value(value) {}    void SomeFunc();    int value;};class B{public:    B(const A &a) : a(a) {}    void foo()    {        std::cout << a.value << std::endl;    }private:    A a;};void A::SomeFunc(){    B b(*this);    b.foo();}int main(){    A a(42);    a.SomeFunc();}
Your code snippet only contains constructor/destructor declarations. Did you give them a definition as well?

Here's an example definition for A's constructor:

A::A(){    cout << "I'm A's constructor!" < endl;}


If that's not it, post the exact error you are getting.
Without knowing what you intend on doing with A and B the easy solution is to create a thrid class C. And make C the base class that A and B derive from.

Class C{ C();};Class A : public C{ A(C &c);};Class B : public C{  B(C &c);};


And make sure that whatever fields you need from A in B.. and from B in A.. exist within C. Thus you can generate a Class C, and use that to generate A and B as necessary. Also, A and B both do not require eachother to build or compile, and they'll have a common naming scheme for all shared data (represented by class C).

Hope that helps.

This topic is closed to new replies.

Advertisement