member function or not?
-
class A
{
public:
...
AddTo(CList &L);
private:
//data
}For the bad, A is couple with CList. For the good, A knows the knowledge to fill CList. But I think it's not necesary for A to know CList. Is it better to write a gobal function, like:
bool FillList(CList &L, const class &A);
-
class A
{
public:
...
AddTo(CList &L);
private:
//data
}For the bad, A is couple with CList. For the good, A knows the knowledge to fill CList. But I think it's not necesary for A to know CList. Is it better to write a gobal function, like:
bool FillList(CList &L, const class &A);
Why do you want to write a function for that ? CList probably has a function to do that (are you talking about MFC CList?). If not, then you should put this function in CList.
Cédric Moonen Software developer
Charting control [v1.4] -
Why do you want to write a function for that ? CList probably has a function to do that (are you talking about MFC CList?). If not, then you should put this function in CList.
Cédric Moonen Software developer
Charting control [v1.4]For polymophism,
class A {
public:
virtual void Fill(CList &L);
}class B : pubic A
{
public:
virtual void Fill(CList &L);
}class C : pubic A
{
public:
virtual void Fill(CList &L);
}If I get a object refereced by
A *p
, I may callp->FillList(L)
. In another sense, the object knows what to add to the list, it's a reason to add such a memeber function. The bad thing is that they are coupled. -
For polymophism,
class A {
public:
virtual void Fill(CList &L);
}class B : pubic A
{
public:
virtual void Fill(CList &L);
}class C : pubic A
{
public:
virtual void Fill(CList &L);
}If I get a object refereced by
A *p
, I may callp->FillList(L)
. In another sense, the object knows what to add to the list, it's a reason to add such a memeber function. The bad thing is that they are coupled.The list handles perfeclty adding polymorphic objects to itself. So, what's the problem ? Why do you absolutely want to have a function in class A ? That doesn't make a lot of sense to me. Of course, your list should store pointers to A, then you can add B or C objects to the list and keep the power of polymorphism.
Cédric Moonen Software developer
Charting control [v1.4] -
The list handles perfeclty adding polymorphic objects to itself. So, what's the problem ? Why do you absolutely want to have a function in class A ? That doesn't make a lot of sense to me. Of course, your list should store pointers to A, then you can add B or C objects to the list and keep the power of polymorphism.
Cédric Moonen Software developer
Charting control [v1.4] -
I haven't say it clearly. The case is that class B or C has a lot a members to add to the list. class B : public class A { public: virtual void AddToList(CList &L); private: long m_var1; long m_var2; long m_var3; }
followait wrote:
The case is that class B or C has a lot a members to add to the list.
:wtf: Ooookayy. That was not clear at all from the begining: you don't want to add the object itself to the list but the object should add some elements in the list. In that case, I would make it a member function of the class.
Cédric Moonen Software developer
Charting control [v1.4] -
I haven't say it clearly. The case is that class B or C has a lot a members to add to the list. class B : public class A { public: virtual void AddToList(CList &L); private: long m_var1; long m_var2; long m_var3; }
Class A only needs to have a method AddToList. I am visualizing from example that List is going to contain only pointer of Class A. Then base class object can point derived class object so basically this method not need to make virtual. Even though if list is going to have copy of object then need to implement copy constructor & assignment operator in each derived class to make appropriate copy. But from my point of view class responsibilities should not be mixed up it should be some controller class responsibility to collect object in list rather then Class A itself. So writing a separate method in any other class is better from design-wise. Or make a another class derived by CList and create a method e.g say: "CollectDataInList(A *a)" or have this method in class which is containing CList object.
Akash