Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Passing an object into another (http://www.go4expert.com/forums/passing-object-t3171/)

checkerboard 27Feb2007 15:15

Passing an object into another
 
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.

shabbir 27Feb2007 17:15

Re: Passing an object into another
 
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


All times are GMT +5.5. The time now is 18:32.