Two classes share common data - How to?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viettrung
    New Member
    • Apr 2008
    • 4

    Two classes share common data - How to?

    Hi,

    I have two classes that share a common data list (specifically, a std::vector). This data list should be accessed by the two classes only, so I think using a global variable is not a good solution. Actually I am so confused to find an elegant way to store such common data for the two.

    If you have any idea about that, please let me know.

    Thanks alot!

    .viettrung.
  • uofm
    New Member
    • Apr 2008
    • 1

    #2
    How about this:

    #include <vector>

    class A
    {
    private:
    std::vector< int > m_vCommon;

    public:
    A( void ){};
    ~A( void ){};
    std::vector< int >* GetCommonVector Ptr( void )
    {
    return &m_vCommon;
    };
    };

    class B
    {
    private:
    std::vector< int >* m_pvCommon;

    public:
    B( void ): m_pvCommon( NULL ){};
    B( A& a )
    {
    m_pvCommon = a.GetCommonVect orPtr();
    };
    ~B( void );
    };

    Is that what you want?

    Comment

    • viettrung
      New Member
      • Apr 2008
      • 4

      #3
      Hi uofm,

      Thanks so much for your solution.

      Actually I have thought about this solution before, but one point makes me not totally satisfied is that class A and B seem to be closely coupled to each other. Specifically class B should know about A to get the common data. Do you think so?

      By the way, I have just googled for this matter and found out something called Monostate Pattern which, I think, may solve this matter better. Please see the following article for details: http://www.devx.com/getHelpOn/10Minu...16361/0/page/1

      If you have interests in it, please give me your opinions about that. I would much appreciate.

      Thanks again for your time.

      .viettrung.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        If Class A and Class B are so closely related to each other, maybe they could be subclasses of the same superclass, Class C. Then Class C has the data both A and B need, and therefore each can access it.

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          The monostate design pattern only uses static variables for sharing information. This means that ALL class A & B objects share the same data and in your case, they would all share the same vector.

          The solutions by uofm and ganon allow any two objects to share the same data. Here you would pass the list from one object to another.

          Both are viable soultions, and you'll have to figure out which one works best for you.

          Comment

          • viettrung
            New Member
            • Apr 2008
            • 4

            #6
            Hi Ganon11,

            Thanks alot for you suggestion. That is a good idea. But in my case, the two classes have no relationship else but sharing the common data list. So logically I do not want them to share the same parent.

            Have a good day!

            .viettrung.

            Comment

            • viettrung
              New Member
              • Apr 2008
              • 4

              #7
              Hi RRick,

              Your explaination is very clear for me to see the advantages of the three solutions in this matter. I will consider about them. Many thanks!

              @all: Again, thank you very much for your support. I have learned many valuable experiences from you.

              Best regards,

              .viettrung.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by uofm
                class A
                {
                private:
                std::vector< int > m_vCommon;

                public:
                A( void ){};
                ~A( void ){};
                std::vector< int >* GetCommonVector Ptr( void )
                {
                return &m_vCommon;
                };
                };
                Actually, this is not a good solution. Class A has broken encapsulation by returning a pointer to its private data member. That means the data member can be altered by functions that are not class A functions. That means there is no control of the contents of the database.

                In effect this is a global variable parading as a member variable.

                When you have common data, you need to ask whether the data can change independently from the class objects. If this is the case, the object should have a pointer (preferably a handle) to the data.

                The data itself is managed by another object altogether.

                [code=cpp]
                class DataManager
                {
                private:
                vector<int> theData;
                public:
                SetTemperature( int arg);
                int GetMaxTemperatu re();
                //etc...
                };
                [/code]

                Here the vector contains temperatures. The SetTemperature( ) can regulate what goes in the data be refusing input like 263634673 degrees. Merely exposing a pointer to the vector destroys this ability.

                class B now has a pointer to a DataManager object. It is the DataManager that provides access to the data through its public interface.

                You really want to avoid closely coupled classes since it is the path to monolithic code.
                Last edited by Ganon11; Apr 12 '08, 07:44 PM. Reason: Fixing [CODE] tags.

                Comment

                Working...