#include <stdio.h>
class A;
class B;
class A
{
public:
int var1;
void Change()
{
binst.var2 = 10;
}
};
A ainst;
class B
{
public:
int var2;
void Change()
{
ainst.var1 = 20;
}
};
B binst;
int main()
{
printf(" var1 = %d\n",ainst.var1);
printf(" var2 = %d\n",binst.var1);
//let's change and see new things
ainst.Change();
binst.Change();
printf(" var1 = %d\n",ainst.var1);
printf(" var2 = %d\n",binst.var1);
return 0;
}
🔒 Locked
Class forward declaration problem
Advertisement
Original Post
Well I encountered a problem , Maybe someone could help me out ? What I want to do is to access other class variables or functions . Here is the code example I have came up with : C++ The problem is in the A class it can't see the binst (binst' : undeclared identifier) declaring it before the A class , produce compiler error like : ('binst' uses undefined class 'B'). So what is the best way to solve it ? Thanks in advance.
Don't inline your code.
I should point out now that having 2 classes that are dependant on each other is a bad design flaw to start off with. having them dependant on global variables of each other is even worse :).
class A{public: void Change (); int var;};class B{public: void Change (); int var;};A ainst;B binstvoid A::Change (){ binst.var = 555;}void B::Change (){ ainst.var = 556;}void main (){ ainst.Change (); binst.Change ();}I should point out now that having 2 classes that are dependant on each other is a bad design flaw to start off with. having them dependant on global variables of each other is even worse :).
Separate declaration from definition, and it should work.
Cheers
~matt();
#include <cstdio> // and use the right headers!class A{public: int var1; void Change();};class B{public: int var2; void Change();};A ainst;B binst;void A::Change(){ binst.var2 = 10;}void B::Change(){ ainst.var1 = 20;}int main(){ printf(" var1 = %d\n",ainst.var1); printf(" var2 = %d\n",binst.var2); // and use the correct members //let's change and see new things ainst.Change(); binst.Change(); printf(" var1 = %d\n",ainst.var1); printf(" var2 = %d\n",binst.var2); // and use the correct members return 0;}Cheers
~matt();
Is there an echo in here? ;)
Tight coupling (ie. this) is a pain in the arse. A simple solution is to have each class store a pointer to the global object which can be set via a SetClassBSource/SetClassASource function. This means that the objects are not ties to global variables, which will help reduce coupling (which is a Good ThingTM).
I suggest you get yourself a copy of Code Complete 2nd. Edition. This book has litteraly changed the way I work and program. It's one of the best general-purpose books on software construction out there and has very good definitions of these types of problems. It also has a gazillion (yes that many) book references at the back, and just about all of them are classics.
I believe someone in these forums said it best: "Live and die by Code Complete". I couldn't agree more :).
I suggest you get yourself a copy of Code Complete 2nd. Edition. This book has litteraly changed the way I work and program. It's one of the best general-purpose books on software construction out there and has very good definitions of these types of problems. It also has a gazillion (yes that many) book references at the back, and just about all of them are classics.
I believe someone in these forums said it best: "Live and die by Code Complete". I couldn't agree more :).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement