# Passing objects as references and copies

This topic is 2111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello!

Just wondering. If i set up a pointer to an object, and pass what the pointer is pointing to, is this still a reference to the object or is it a copy?

Example:

class object {
variables...
}

object        *pointer;
object        copy;

void someCopyFunction(object);

/* Passed as a copy */
someCopyFunction(copy);

pointer = new object;

/* Passed as... what? */
someCopyFunction(*pointer);

Edited by Zomgbie

##### Share on other sites

As your code looks like C and not C++ and references dont exist in C its both times a copy(assuming that code compiles as is).

If it was C++ you would have to look at the declaration of someFunction to see if the argument was declared as a reference.

##### Share on other sites

Old habbit as i'm used to working with C. Currently switching over to C++ for a new project. I've updated the example to be more C++ specific :)

##### Share on other sites

The second function call uses -- in some way -- both. The pointer is copied, but the object (i.e. what the pointer points to) is not copied, it is only "referenced" by the pointer.

If sizeof(object) is considerable, this may be a valid (micro-) optimization since obviously copying a 4 byte (or 8 byte) pointer is less work than copying a 96 byte object. For small objects, however, it's not worth bothering.

If you pass several objects by pointer, you will want to make them restrict (if you can, that is if they're not aliasing). Otherwise, the compiler will be forced to assume that one object could alias another, and it will have to add a lot of extra loads and stores.

If you have C++ available, you might generally want to pass objects as references (that's somewhat similar to using pointers, but not quite exactly the same) or const references if you know you won't modify them.

Often, the compiler internally just secretly uses a pointer anyway when you use a reference, but you have easier syntax because the reference looks and feels like a "real" variable to you (no dereferencing necessary). Then again, I've seen compilers do miraculous optimizations with references many times, entirely eliminating function calls and leaving no evidence whatsoever that anything was referenced or passed as function parameter at all.

Edited by samoth

##### Share on other sites

Parameters are copied (via the copy constructor if one exists EDIT: if there is no copy constructor the compiler will generate one for you if it can, which will be a memberwise copy, like in C) unless you use a reference in C++. (The compiler may optimise it out though).

Your new 2nd example doesn't compile because someOtherFunction expects an object* but you pass *pointer which is of type object. If it took an object (as in your original example) it would copy the object. If it took an object& there would be no copy made.

##### Share on other sites

Parameters are copied (via the copy constructor if one exists) unless you use a reference in C++. (The compiler may optimise it out though).

Your new 2nd example doesn't compile because someOtherFunction expects an object* but you pass *pointer which is of type object. If it took an object (as in your original example) it would copy the object. If it took an object& there would be no copy made.

Ah yes, sorry. Missed some parts while editing the example. Should be correct now.

Edited by Zomgbie

##### Share on other sites

Well that is your answer; it will copy the object pointed to by pointer.

##### Share on other sites

Well that is your answer; it will copy the object pointed to by pointer.

Actually writing it down and discussing it a bit, kinda cleared it out for me. All of the sudden it feels like a silly question now that the answer is so obvious. Thanks for the great input! I'll go grab my morning cup of coffe and initiate my wake up process now...

##### Share on other sites

(scratch that, silly me, I didn't see the pointer was dereferenced in the function call, you're right)

Edited by samoth

##### Share on other sites
Rather than just answering your specific example, the only factor is the function declaration. If the function declaration defines that it receives a reference, it will always be a reference, no matter how you try to pass it. If it receives an object with no reference, it will always be a copy of the object.

It is not related to your use of pointers etc. If function receives a reference, it will be a reference.

L. Spiro

• 13
• 18
• 29
• 11
• 27