Hi, I have a question about how to design a class.
I am really confused about how objects interact with others.
I know most of the syntax of c++, polymorphism, inhertiance, template as well.
But like others said, knowing syntax do not automatically make u a good programmer.
For eg, if after analysis, I choose to define 10 user define type class A, class B, class C, ..., Class J.
Is it normal if I instance all this objects globally?
That means all if method of A wants to communicate B, it can just directly use global instance b1. Is this acceptable in OOP design?
If I use composition, for example:
Sometimes I confront of situation tat operate() method of B needs to know some data of A in order to operate.Code:class B { public: void operate() {dosomething;} private: _data of B; } class A { public: void method1() {_pb->operate();} private: B* _pb; };
But it can't not.
Then a solution I first think of is to add friendship of class A to B;
Seems a really bad idea.
Then I will try to drag B* _pb out of class A
And instance A a1, B b1, globally.
If I confront this. How should I resolve this condition?Code:void A::method1() { b1.operate(); // Call global b1 instance } void A::method1() { a1.methodxxx() // may need to call a1,too }
If the compsition relation is much larger;
like
Does that mean I need to redesign my class?Code:class D { public: void methodD() { need to know some information about class A ??} // what should I do? } class C { public: void methodC() { _pd->methodD();} private: D* _pd; } class B { public: void methodB() {_pc->methodC();} private: C* _pc; } class A { public: void methodA() {_pb->methodB();} private: B* _pb; };



LinkBack URL
About LinkBacks



