I've been reading up on c++ a lot lately and I seem to be understanding everything, except this concept. I can't seem to find information anywhere about how passing an object to a function works. I watched this tutorial:
Haven't watched the video, but you can pass objects to functions just like you would any other variable type:
1 2 3 4 5 6 7 8 9 10 11 12
struct POD {
int x, y;
Pod(int X, int Y) : x(X), y(Y) {};
}
int sumXY(POD p) {
return p.x+p.y;
}
int main() {
POD myPod(4,5);
cout << "Value of x + y: " << sumXY(myPod);
return 0;
}
Just like with basic types, you can pass objects by value (but you generally shouldn't, since copying large objects is expensive), pointer or reference.
First, do you know how passing in a primitive type to function work? That is passed in a int,char etc into a function? If yes, then passing in object is similar. The only difference is the object is composed of many primitive/object types internally. Since composed of many primitive/object types it is important for performance not a whole copy of the object is passed in but a reference or a pointer of an object into it.