Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualBrother Bob

Posted 10 February 2012 - 01:05 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.

#1Brother Bob

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.

PARTNERS