Passing an object into another

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

  1. checkerboard

    checkerboard New Member

    Feb 12, 2007
    Likes Received:
    Trophy Points:
    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

    Jul 12, 2004
    Likes Received:
    Trophy Points:
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice