| | |
passing objects as function parameters
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






