passing objects as function parameters

Reply

Join Date: Jul 2008
Posts: 1
Reputation: sarvari is an unknown quantity at this point 
Solved Threads: 0
sarvari sarvari is offline Offline
Newbie Poster

passing objects as function parameters

 
0
  #1
Jul 20th, 2008
hhey sir i need some information about his topic urgently
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: passing objects as function parameters

 
0
  #2
Jul 20th, 2008
what do you want to know about it? c++ objects are normally passed by reference to avoid expensive duplication and to let other functions use the same object as the calling function.
  1. class MyClass
  2. {
  3. // blabla
  4. };
  5.  
  6. void foo(MyClass& obj)
  7. {
  8. // class passed by reference
  9. }
  10.  
  11. int main()
  12. {
  13. MyClass myclass; // create an instance of the class
  14. foo(myclass); // pass it to another function
  15. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: passing objects as function parameters

 
0
  #3
Jul 20th, 2008
They can also be passed as pointers, if you want to use them. Thought I believe it's considered better to use references over pointers when possible.
  1. class MyClass
  2. {
  3. // blabla
  4. };
  5.  
  6. void foo(MyClass* obj)
  7. {
  8. // class passed by reference
  9. }
  10.  
  11. int main()
  12. {
  13. MyClass myclass; // create an instance of the class
  14. foo(&myclass); // pass it to another function
  15.  
  16. //or....
  17. MyClass* myclass = new MyClass;//create a pointer to the class and allocate an object at its location
  18. foo(myclass);//call the function
  19. delete myclass;//deallocate the object
  20. }
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC