Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we have to use a reference in argument of copy constructor instead of a pointer?

Tags:

Why we have to use a reference in argument of copy constructor instead of a pointer? This questions was asked in an interview. I replied the following points:

  1. References can not be NULL.
  2. If we use pointer, then it would not be a copy constructor.
  3. The standard specifies so (section 12.8.2).

But the interviewer was not satisfied. He said it was 'related to inheritance and virtual functions'. I need help or some relevant points with which I can be able to think of an answer in that direction.

like image 465
karan Avatar asked Aug 30 '16 03:08

karan


2 Answers

You points look sensible, and I cannot figure out what your interviewer wanted to hear from you.

But, as far as I know, enabling a uniform syntax for copy constructors and assignment operators was a significant reason why Stroustrup introduced references. For example, let's assume we have no references in C++ and we want to do some copy-construction, passing an argument by pointer:

class MyClass {
    // Copy ctor and assignment operator are declared
    // like this in our referenceless "C with classes" language:
    MyClass(const MyClass* const);
    MyClass* operator=(const MyClass* const);
};

MyClass a, b, c;

//Let's copy-construct:
MyClass d = &a; // Dereferencing looks ugly.

//Let's assign:
a = &b; // Dereferencing again.

// Even worse example:
a = b = c = &d; // Only the last variable should be dereferenced.
a = b = &c; // Awfully inhomogeneous code.
/*
The above line is equivalent to a = (b = &c),
where (b = &c) returns a pointer, so we don't need to
dereference the value passed to assignment operator of a.
*/

And this is obviously not how built-in types work:

int a, b, c;
int d = a;
a = b = c = d;

So references were introduced to resolve these inhomogeneities.

UPD

For an authoritative proof of this point, please refer to the § 3.7 of Stroustrup's The Design and Evolution of C++. Unfortunately, I have no English text of the book, but the first sentence in the chapter is (back-translated into English):

References were introduced basically for operator overloading support.

And in the next few pages Stroustrup covers the topic in-depth.

like image 186
Sergey Avatar answered Oct 08 '22 08:10

Sergey


Your interviewer was wrong, and you are correct.

The most important aspect of your answer is 2. and 3 (which are essentially the same points, but worded differently): because the standard says so (see 12.8.2):

A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (...).

We use references instead of pointers because the standard says so. It's not a copy constructor otherwise.

When it comes to inheritance and virtual functions, references can do everything that a pointer can.

The only other reason you could've provided, as Gautam Jha points out in the comments, is that a reference allows you to make a copy from temporaries.

like image 37
Tas Avatar answered Oct 08 '22 09:10

Tas