11

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Madu
  • 4,849
  • 9
  • 44
  • 78
  • 4
    You cannot pass by value to a copy constructor, because to pass by value you have to make a copy, and you cannot make a copy because that would involve calling the copy constructor that needs a copy that needs the copy constructor that – n. m. could be an AI Apr 03 '12 at 18:45

6 Answers6

13

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.

spencercw
  • 3,320
  • 15
  • 20
6

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).

justin
  • 104,054
  • 14
  • 179
  • 226
  • I think the wording "saves a copy" might be a little confusing (as in "keeps a copy"). I took a double take before I realized you meant "prevents a copy" – Eric Apr 03 '12 at 18:43
  • Would there ever be an instance where `Book::Book(const Date date)` would be used over `Book::Book(const Date &date)`? i.e. is passing the object directly (omitting the ampersand) but as a `const` ever preferred? – Jens Bodal Oct 01 '14 at 23:42
  • @akevit i'm only thinking of cases which would aid questionable designs; e.g. unexpected copy semantics, intransitive const, tip-toeing around potentially aliased memory, moving const objects. of course, some people will use const values when the type costs (next to) nothing to copy -- which is fine. – justin Nov 21 '14 at 07:52
2

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());
Attila
  • 28,265
  • 3
  • 46
  • 55
1

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.

zdan
  • 28,667
  • 7
  • 60
  • 71
1

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.

DataPlusPlus
  • 1,090
  • 1
  • 7
  • 11
0

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.

tmpearce
  • 12,523
  • 4
  • 42
  • 60
  • Is the inclusion of the ampersand purely for optimization then? For example `Book::Book(const Date &date)` would pass the reference of the object as well as prevent alteration of the original object, however `Book::Book(const Date date)` would pass the actual original object but also prevent alteration? – Jens Bodal Oct 01 '14 at 23:43
  • 1
    @akevit See [this question and answers](http://stackoverflow.com/questions/8714250/isnt-const-redundant-when-passing-by-value) regarding your question. – tmpearce Oct 02 '14 at 02:40