1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Passing an object into another

Discussion in 'C' started by checkerboard, Feb 27, 2007.

  1. checkerboard

    checkerboard New Member

    Joined:
    Feb 12, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    When we pass an object (say a) of Class A to another object (say b) of class B, what is the benefit of that. I understand that b now has access to the public functions of a.

    Therefore, say I need to calculate a value of b using some values calculated by functions in a.

    1) I can either, in Main, calculate the values from a and pass it as arguments into the function of b OR
    2) I can access the function of a within the function of b to calculate the values i need.

    I do not really see that why the second one should be preferred if indeed it is. The first can be achieved without passing one object into another.

    Any particular reasons why passing objects into another object is superior. All it essentially save is the number of variables we pass into functions and some space on Main.

    Pls advise.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    1. Because it is needed. Dont quote on me but what you are saying is a special scenario where just some values are needed but what if the calculation of A is dependent on the settings that B has.

    2. Thats better way to organize the code. Say I call the A's function 3 times then I need to calculate the b's value 3 times unnecessarily. You can quote me on this :P
     

Share This Page