It has been a long time since I have used C++ (I am only 20, so maybe not that long) I have been using several languages in the last couple years(Java, ActionScript2 and 3, PHP), polluting my mind with many different idioms, and now need to hop back to C++.

Many questions have been coming to mind while I am trying to get back into the groove, and one thing that is causing some confusion is that functions pass arguments by value. This makes perfect sense for primitives, but I am getting lost in the semantics of pointers and references when it comes to structs and objects.

Now, arrays and pointers can be used "interchangeably", because an array IS a pointer to its first element (correct me if I am wrong). Is the same true of structs and objects?

Date dt = new Date();


void myFunc1(Date d)

void myFunc2(Date & d)

void myFunc3(Date *  d)

So, if I am understanding correctly, when myFunc2 is invoked, it creates a Date reference and then assigns dt to the new reference(Constructor not called). Also the same for myFunc3, only we are using a pointer instead of a reference.

My confusion is in myFunc1. What is happening? If you pass by value, exactly what value does Date dt contain (a pointer?)? Is Date d a local copy of Date dt?

I am horribly comfortable with pass by reference. And for all intents and purposes, it seems that when you pass pointers and references, you are in essence passing by reference, although this reference is implicit, and is not a feature of the C++ framework (meaning it must be carefully managed, whereas in Java, there is no chance the object you reference will be unallocated or magically changed to another object)

If you can provide any insight into how structures and classes work internally, and how one goes about passing copies, references, and pointers to them.

Thanks for you time

8 Years
Discussion Span
Last Post by Ancient Dragon

>>My confusion is in myFunc1. What is happening? If you pass by value, exactly what value
>> does Date dt contain (a pointer?)? Is Date d a local copy of Date dt?

Yes. structures (classes) can be passed by value just like POD (plain-old-data) types. The ocmpiler makes a copy of the structure and puts that into the parameter of the called function. Whatever changes the called function makes to the structure or class are NOT reflected in the copy of the calling function.


Thanks for your reply. I have another question regarding the specifics of how that process works.

Does Date dt pass a pointer (or reference) and then myFunc1 dereferences and copies through some internal copy function, or is the entire structure pushed onto the stack, and then myFunc1 pops the data off the stack and places it into its own Date d.

I am curious as to how the compiler handles structures. Are there existing mechanisms invisible to the programmer that do the work for you, and if not, how does the compiler go about maintaining the integrity of structures.


Since Dt is passed by value then it means the entire structure is pushed onto the stack (in most implementations) when Function1 is called. That's what pass by value means.


Thanks again.

I was getting confused because I wasnt exactly sure what the value of a structure is. Is the value of a structure the structure itself, or is there some invisible level of abstraction that makes it work out that way. (coming from Java has been making this difficult to grasp)


The compiler pushes the value of each variable contained in the structure onto the stack as if they were individual integers (or whatever POD they happen to be). But C or C++ programmers don't really have to be concerned about that (at first) because inside function1() it is treated as a structure, not as individual elements of the structure.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.