Jump to content
  • Advertisement
Sign in to follow this  
b1gjo3

2 classes that are dependant on each other

This topic is 3680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how can i have something like this
class A
{
public:
	A(B);
	~A();
};

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);
//...
}

Share this post


Link to post
Share on other sites
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);
//...
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!