Hi,

I do have doubt regarding const declaration in copy constructor. Can anyone let me know why do we need
const reference in copy constructor, as we already know reference itself is const pointer.So why we should use
const as a prefix in copy constructor.

Regards,
Mohan

Recommended Answers

All 4 Replies

There are really only three options (in C++03, pre-C++11) to pass a parameter: by value, by reference or by const-reference. In the case of the copy-constructor, you cannot pass the parameter by value because to be able to do the copy (of the passed object), you need a copy-constructor, so that's impossible. The remaining options are by reference or const-reference, which I think you know already.

So, this boils down to the difference between a reference and a const-reference. As you said, a reference is a kind of const-pointer in disguise. However, like a pointer, it can refer to either a const or non-const object. So, what a const-reference really means is that you obtain a reference to an object that you are not allowed to modify, which is usually fine for a copy-constructor (i.e., a "copy" usually should not modify the original object). And as per the principle of minimum requirements, you should not require that the object be modifiable if you really don't need to modify it, and that is why you would almost always use a const-reference for the parameter of the copy-constructor (although, technically, you could use a non-const reference too, but you will have some rvalue/lvalue issues).

A const-reference is generally preferred whenever the object could be expensive to copy (or cannot be copied at all, like when it is a parameter to a copy-constructor) and all you really need is to be able to "look" at the object (read-only operations). Non-const references should only be used if you really want to modify the object that is passed to the function as a form of output of your function.

I suggest these FAQs as further reading on constness issues.

@mike:I just wanted to know being const pointer , what is the need to pass the const reference. i believe it should have passed without const . i have read so many places that reference is const pointer. Could you please through some light on this .

A reference is a constant pointer in the sense that the reference cant be changed to point to something else. You can still change the value that reference points to. On the other hand if you pass a const reference then you cant change the value that the reference holds.

i have read so many places that reference is const pointer.

Not so much. While it may help to think of references as constant pointers where indirection is done for you, eventually that relation will fall short. Here are a couple of notable differences:

  • A const pointer can be a null pointer while there's no such thing as a null reference.
  • A pointer has a unique address. A reference has the same address as the referenced object.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.