Ampersand & with const in constructor

你说的曾经没有我的故事 提交于 2020-01-12 07:55:25

问题


Can some body tell me the reason why we usually put const and & with some object which is passed in the constructor for example.

Book::Book(const Date &date);

The confusion that i have here is that usually & sign is used in the some function because the value is passed by reference and whatever changes happen to that variable in the function should reflect afterwards. But on the other hand const says that no assignment can be done to that variable.

If some body have some good idea about that please let me know the reason for that.


回答1:


This is done to avoid an unnecessary copy. Take, for example, the following code:

Book::Book(Date date):
date_(date)
{
}

When you call this constructor it will copy date twice, once when you call the constructor, and once when you copy it into your member variable.

If you do this:

Book::Book(const Date &date):
date_(date)
{
}

date is only copied once. It is essentially just an optimisation.




回答2:


The most common alternative is to pass by value:

Book::Book(Date date);

Passing by const reference prevents the parameter date from being copied when the parameter you pass is already a Date. Copying objects can be unnecessary, can be costly to perform, or it could result in a sliced object (and the incorrect results).

'Slicing' is basically demotion of an object's type via copy to its base. For a polymorphic type, this can actually change its behavior because the parameter would be copied as its base (Date), and then calls to its polymorphic interfaces would be different because the implementation has changed (e.g. its virtual methods would use the base's implementation instead).




回答3:


It means that you pass the object via refrence (as you noted), but the object itself cannot be changed from the function (ctor in this case).

The reason for this could be:

  • you do not want to copy the full object, to make your code more efficient
  • you do not want to accidentially change the passed-in object
  • you want to be able to use the function with unnamed temporaries
  • you want to be able to pass objects that derive from the noted type (Date in this case)

For the third point, consider:

Book b(Date());



回答4:


A const reference is a way of passing the data to the class without copying the data to a local copy, and still guaranteeing that the original object won't be modified by the function.




回答5:


It's typically a performance optimization for input parameters. If you omit the '&' the parameter is accepted by value and the input object will have to be copied before being passed to the function. Passing by reference bypasses the copy.




回答6:


In c++, when you have a parameter type of a function be something like const Type&, what you are doing is allowing a user to pass some value in by reference - A pointer to the value is implicitly passed in, but for ease of use, the compiler allows you to treat it as if it was a value.

In some cases, the compiler can also optimize it so that no pointer is used at all, and the function can refer directly to the memory of the value.

The reason that const is used is to safeguard yourself from altering memory that the user doesn't expect you to alter, and also to have it still work if the user passes in a const variable.



来源:https://stackoverflow.com/questions/9999283/ampersand-with-const-in-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!