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

前端 未结 7 555
温柔的废话
温柔的废话 2020-12-30 12:18

In java and objective-c, a variable representing an object is generally a pointer to that object. However, it seems that in C++, it\'s common to have non-pointer types hold

相关标签:
7条回答
  • 2020-12-30 13:06

    When you pass an object to a function by value, it's copied by means of its class's copy constructor. If you haven't defined a copy constructor, there'll be a default one supplied by the compiler (unless you take special steps to avoid that), which is equivalent to copying the members by hand.

    The preferred thing to do is often actually to pass a const reference rather than either a pointer or the object itself.

    (You might want to be aware that actually a struct is just a class whose members are public by default; in particular, structs can have user-defined copy constructors too. A struct is not necessarily just plain inert data.)

    0 讨论(0)
提交回复
热议问题