| | |
passing objects as function parameters
![]() |
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.
C++ Syntax (Toggle Plain Text)
class MyClass { // blabla }; void foo(MyClass& obj) { // class passed by reference } int main() { MyClass myclass; // create an instance of the class foo(myclass); // pass it to another function }
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.
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.
C++ Syntax (Toggle Plain Text)
class MyClass { // blabla }; void foo(MyClass* obj) { // class passed by reference } int main() { MyClass myclass; // create an instance of the class foo(&myclass); // pass it to another function //or.... MyClass* myclass = new MyClass;//create a pointer to the class and allocate an object at its location foo(myclass);//call the function delete myclass;//deallocate the object }
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
![]() |
Similar Threads
- urgent help ith c++ program (C++)
- urgent help with c++ function access(/cpp) (C++)
- Generic method parser / invoker (C#)
- currency interest program need help coding it further (C++)
- Long ado net question but urgent help required (ASP.NET)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
- Object-Oriented Programming (C++)
Other Threads in the C++ Forum
- Previous Thread: TXT File Loading Prog
- Next Thread: causes memory leak...?
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets






