Archived

This topic is now archived and is closed to further replies.

graveyard filla

question about classes????

Recommended Posts

high, i have a question, why do we sometimes send an object to a function? i know its to get access to that objects variables and inner functions, but couldnt we have access to them anyway? do you only send the object when you want to change the value of that objects variables? what about inner functions? is there any reason to send the object if your just using its functions? when do you have to send it and when can you just use it? thanks for any help.

Share this post


Link to post
Share on other sites
Zahlman    1682
To talk to an object (instance ), you have to know about it. So if I want to call its methods, then I have to have access, which can be acheived in a variety of ways:

- the object is a global instance.
- (better than the above, generally) There is a static method in the class which lets you access the instance (Singleton pattern).
- You create the object yourself and thus own it.
- The object is a member of this , in a method (doesn''t work in a normal function in C++, since there is no this there)
- The object is passed in as a parameter.

Share this post


Link to post
Share on other sites
Mister Stewart    122
You do it so you can act on the object within that function - access its attributes (data) and call its methods.



"Dear MacGuyver, here is a paper clip, a rubber band and a straw - please save my dog."

Share this post


Link to post
Share on other sites