In C ++, what's the difference between an object and a pointer to an object?

In java and objective-c, a variable representing an object is usually a pointer to that object. However, it seems that in C ++ typically there are no pointer objects for storing objects. What is the difference between the two?

If I pass a struct as an argument to the function, I assume that I pass the value, that is, I actually create a new structure in memory, and changes to this structure inside the passed function will not affect the "source" structure outside the function. However, if I pass a pointer to a structure, only one original structure remains, and changes to the structure referenced by the pointer will be visible to any code that knows about this structure. Do I have this right?

So, is there any difference with objects? When I pass an object without a function pointer, is the whole object copied?

+7
source share
6 answers

This is exactly as you said.

When you pass an object by value, its copy constructor is called to create a new instance of such an object, which will be used inside the function. Changes made to this new facility will not be reflected in the original 1 .

As with structures, the default copy constructor simply executes a shallow copy of the original object, that is, its fields are copied to a new instance 2 ; in many cases this is undesirable (for example, if the object wraps a pointer / other resource), therefore there are classes that override the copy constructor or completely disable it. The objects of these last classes can only be passed in by pointer or by reference.

Passing objects by value can be expensive if they are larger than a pointer (in size) or even if their copy constructor is not "cheap." Compared to pointers, on the other hand, throughput provides the usual advantages of not having to point to a pointer, letting the caller do whatever they want with the object, etc.

Note that passing an object by value kills polymorphism. This is because the function that receives the object by value receives a statically typed object with the exact size and type, so any attempt to pass an object of a derived class will split the objects (the copy constructor for the base class is called, which by default simply copies the fields available in the base class).

This is why the often preferred method for passing objects is through a const reference. This has several advantages:

  • no copies; the object that the called person will see will be exactly the one indicated at the time of the call;
  • No changes to the original object can be made, thanks to the const qualifier;
  • if the called call needs to change a copy of the object, it can still create a copy from the link;
  • lack of fuzzy pointer syntax;
  • polymorphism persists, since behind the scenes we actually transmit the pointer;
  • There is little doubt that the object belongs to: the general rule about links is that they belong to the caller.

  • As for the "raw fields" of the object; Naturally, if the source object and the copy continue to share the pointer / descriptor on the same resource, some changes may affect others.

  • Primitive types (and POD in general) are copied bitwise, and the copy constructor is called for non-POD types.

+15
source

The difference is mainly related to where the object is allocated in memory. For example:

 int main() { MyObject x; //allocates space for an instance of MyObject on the stack MyObject* y; //allocates space for a pointer on the stack MyObject* z = new MyObject(); //allocates space for a pointer on the //stack and an object instance in the heap and //sets the pointer to point to the new instance MyObject* a = &x; //allocates space for a pointer on the stack and //makes it point to 'x' ... } int someFunc(MyObject byValue, MyObject* byReference) { //the 'byValue' parameter will be passed by creating a copy of the //entire source object on the stack (can be quite expensive for //complex object types) //the 'byReference' parameter will be passed by creating a //copy of the source pointer on the stack and setting it to //point to the source object in memory } 
+3
source

In C ++, a variable is a variable that it represents. This is the actual object in memory, in the actual place.

However, you can choose that such a variable represent a pointer instead, and in this case it will say: "Hey, I - I, I point there! The object you want is not here, it is THERE Yes, there! Go on, go! "

If you are not explicitly using the C ++ "reference type", which I suspect you are not, then ALL the arguments you pass in are relevant.

+1
source

The answers to the questions in your second and third paragraph are yes. More specifically, if you pass an object to a function by value, the function will receive a copy of that object (created by the copy constructor).

0
source

When you pass an object to a function by value, it is copied using its class copy constructor. If you have not defined a copy constructor, a default compiler will be provided unless you take special steps to avoid this), which is equivalent to manually copying elements.

It is preferable to often pass a reference to const, rather than the pointer or the object itself.

(Perhaps you should know that in fact a struct is just a class whose members are public by default, in particular, structures can have custom copy constructors. A structure is not necessarily just simple inert data.)

0
source

You have this right.

Indeed, how it works. The pointer stores the memory address of the variable.

When you pass a pointer (to an object) for a function as a parameter, this means that the function will have access to this object through its memory address instead of the new object being created on the stack.

Open this thread for more information on pointers.

0
source

All Articles