Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. member function or not?

member function or not?

Scheduled Pinned Locked Moved Design and Architecture
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    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);

    C 1 Reply Last reply
    0
    • F followait

      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);

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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]

      F 1 Reply Last reply
      0
      • C Cedric Moonen

        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]

        F Offline
        F Offline
        followait
        wrote on last edited by
        #3

        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 call p->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.

        C 1 Reply Last reply
        0
        • F followait

          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 call p->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.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #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]

          F 1 Reply Last reply
          0
          • C Cedric Moonen

            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]

            F Offline
            F Offline
            followait
            wrote on last edited by
            #5

            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; }

            C M 2 Replies Last reply
            0
            • F followait

              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; }

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              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]

              1 Reply Last reply
              0
              • F followait

                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; }

                M Offline
                M Offline
                Member 2767084
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups