Design question, sharing data

This is a discussion on Design question, sharing data within the C++ Programming forums, part of the General Programming Boards category; Hi guys so 2 classes need to share information. Class A has information class B needs to modify, and it ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Design question, sharing data

    Hi guys so 2 classes need to share information.


    Class A has information class B needs to modify, and it should also modify class A's data as well.

    Currently this is how its done:

    Code:
    class A {
    int data;
    B b;
    
    public:
    void startUp();
    
    
    }
    
    Class B{
    initParms(int &);
    
    }
    
    
    A::StartUp()
    {
    
    b.InitParms(data)
    }

    As you can see this is how they are sharing data...but is this the best way about doing this or is there a better way?


    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Since your class A contains the B, it would probably be better for class B to provide a function that returns required value. That can be called as required by class A. Particularly since it appears there is only one int involved.

    From the names of functions, (initParms() and StartUp()) it appears the various operations are associated with some form of initialisation of both objects. That suggests it might be better for the relevant work to be performed in relevant constructors of A and B - and not bother with those extra functions at all.

    There's no universal "right" answer to this sort of thing. However, the rule of thumb I suggest is to make things as simple as possible.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Thanks Grumpy,

    In reality, class a has several values that B must use, I was just making it simple.

    When you said make class B return required value I wasn't sure what you ment by this...class B gets its values from class A. Class B modifies those values and those modifcations must be refelcted in class A as well, thats why the initParms() function is pass by reference and not value.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Quote Originally Posted by mr_coffee View Post
    In reality, class a has several values that B must use, I was just making it simple.
    Doesn't really make a lot of difference to the principle. Since you specified the functions with only one integer argument, you simply made the point more obvious.

    Quote Originally Posted by mr_coffee View Post
    When you said make class B return required value I wasn't sure what you ment by this...class B gets its values from class A. Class B modifies those values and those modifcations must be refelcted in class A as well, thats why the initParms() function is pass by reference and not value.
    If class B gets its values from class A, they have to be passed from class A somehow - typically via a function call. That function can accept arguments that specify the data and also return data. Either in the form of data structures or via pointers/references.

    If class B is storing a reference or pointer to data in class A, then the bigger question you need to ask is why the classes are separate in the first place. An A contains a B. The B needs to modify data in the A. What is the point of putting part of A into a distinct class B?

    Making data in a contained object (B) dependent on data in a containing object (A) introduces really strange dependencies in code. Those dependencies tend to mean a change in either class forces a change of the other. If that is the case, you might as well have a class A without a separate class B within it.
    Last edited by grumpy; 02-14-2010 at 04:48 AM.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    I see your point Grumpy. I'm modifying existing code and I guess the original coder thought the new class was different enough from the first class that he needed it to me a whole new class but he just needed some values to initialize the new class.

    But if he is also modifying the values as well it makes you wonder why like you said, he made a whole new class.

    I'll ask him about it on monday. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about class design and overloading
    By l2u in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2007, 01:02 PM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21