-
C++ (
http://www.daniweb.com/forums/forum8.html)
| sarvari | Jul 20th, 2008 1:18 pm | |
| passing objects as function parameters hhey sir i need some information about his topic urgently |
| Ancient Dragon | Jul 20th, 2008 1:34 pm | |
| Re: 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.
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
} |
| CoolGamer48 | Jul 20th, 2008 5:09 pm | |
| Re: passing objects as function parameters 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.
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
} |
| All times are GMT -4. The time now is 10:28 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC