How does passing objects to functions work?

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:

http://www.youtube.com/watch?v=jn3lT07owCo&feature=BFa&list=SPAE85DE8440AA6B83&lf=list_related

as well as its second part and saw it being done, but I can't seem to grasp the concept since it wasn't thoroughly explained in the video.

If someone could clarify how it works I would be greatly appreciative!
Thank you!
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.
Last edited on
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.
Topic archived. No new replies allowed.