Jump to content

  • Log In with Google      Sign In   
  • Create Account

Two classes that calls each other


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Retsu90   Members   -  Reputation: 208

Like
0Likes
Like

Posted 10 February 2012 - 12:47 PM

Hi!
I'm trying to compile unsuccessfully a project that has two classes. The class A has class B as a member, and there is a function where send its pointer to class B. I read already a lot of other sites, forum etc an the trick is called Foward Declaration, but seems to not work... Basically the code is this:
#include <stdio.h>
class A
{
public:
	B b;
	void Init()
	{
		printf("Init A\n");
		b.Init(this);
	}
	void Ok()
	{
		printf("OK A!\n");
		b.Ok();
	}
};
class B
{
public:
	A *a;
	void Init(A *param)
	{
		printf("Init B\n");
		a = param;
		a->Ok();
	}
	void Ok()
	{
		printf("OK B!\n");
	}
};

int main()
{
     A a;
     a.Init();
}


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8575

Like
2Likes
Like

Posted 10 February 2012 - 01:04 PM

Separate declaration and definition.

class A;



class B

{

public:

	A *a;

	void Init(A *param);

	void Ok();

};



class A

{

public:

	B b;

	void Init();

	void Ok();

};



void B::Init(A *param)

{

    printf("Init B\n");

    a = param;

    a->Ok();

}



void B::Ok()

{

	printf("OK B!\n");

}



void A::Init()

{

	printf("Init A\n");

	b.Init(this);

}



void A::Ok()

{

	printf("OK A!\n");

	b.Ok();

}


The forward declaration of A is necessary to define the pointer member and parameter in B. The full definition of B is then necessary so that it can be a member of A. Finally, the member functions are defined once all classes are fully defined.

#3 Retsu90   Members   -  Reputation: 208

Like
0Likes
Like

Posted 10 February 2012 - 01:56 PM

Oh great! So that was the problem, I need to separate the declarations from definitions. Thank you very much, you was clear about the explaining Posted Image

#4 Cornstalks   Crossbones+   -  Reputation: 6991

Like
1Likes
Like

Posted 10 February 2012 - 02:25 PM

Oh great! So that was the problem, I need to separate the declarations from definitions. Thank you very much, you was clear about the explaining Posted Image


To expand, one of the reasons you were probably having issues with forward declaration is because forward declaration only really works for pointer types. So if you were trying to forward declare class B before class A, you'll notice class A creates a full B object, not a pointer to a B object, so forward declaration won't solve the problem for that one. You'll have to fully declare class B before class A.

Now on the other hand, class B uses a pointer to an A object, so forward declaring class A before class B will work. That's why BrotherBob switched the order of class A and class B.

But like BrotherBob said, separating the definitions and the declarations is also necessary here, as he's done.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS