0

I have this simple piece of code :

int a = 1, b = 2;
int* c[] = {&a,&b};
int& d[] = {a,b};
int x = *c[0]+d[1];

If the following works

int &d = a;

I can't understand why the line 3 and 4 generates an error. :-s

It really makes no sense to me :(

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
vladCovaliov
  • 4,333
  • 2
  • 43
  • 58
  • 1
    If you want to ask 2 questions, then feel free two spawn 2 different threads. It's free in SO. Just cross check whether it's asked before or not. – iammilind Jun 28 '12 at 12:03
  • @iammilind: I went ahead and remove the second question and references to "inheritance". I am pretty it is a duplicate anyway, as this shows up from time to time. – Matthieu M. Jun 28 '12 at 12:13
  • @MatthieuM.: And now second part of my answer has no meaning. :( – Alok Save Jun 28 '12 at 12:16
  • @Als: There was not second part when I did it ! – Matthieu M. Jun 28 '12 at 12:16
  • And mine, and the new one. I think the edit should be reverted. – David Hammen Jun 28 '12 at 12:16
  • @DavidHammen: I think the question should be split, and the answers (and bits of) accompany the relevant part. Or we could just close it, I'm just looking for duplicates. – Matthieu M. Jun 28 '12 at 12:19
  • possible duplicate of [C++: why can't we have references to references or array of references?](http://stackoverflow.com/questions/2321578/c-why-cant-we-have-references-to-references-or-array-of-references) – Matthieu M. Jun 28 '12 at 12:20

3 Answers3

4
int& d[] = {a,b};

You cannot create an array of references. It is not allowed by the C++ Standard.For it to be allowed the entire type mechanism would have to be re-written.

int x = *c[0]+d[1];

Gives you an error because the compiler cannot make out the type of d, since there is a compilation error on the previous line where you declare d.


B b[2]; 

Creates two objects of type B hence calls B() twice.

b[1]=D();

Creates an object of type D. For which it calls constructor of the class itself and its Base class, resulting in calls B() and D(). The created object is stored at array index 1. However since the array is of type B Object Slicing takes place and all the derived members of the object are sliced off. Essentially, the object at b[1] is now of the type B

b[1].print();

Calls print() method on object type B(See explanation above why it is type B)

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

for the first part see Als's response. The second part:

  • first two constructors come in for the 2 elements of the array.
  • third B constructor is the base of D
  • then a D constructor
  • then D is being destroyed, together with B since it was a temporary object
  • then the B's print method is called since it is not virtual
  • and then the two B elements from the array are destroyed
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
1

Question #1

int& d[] = {a,b};

You can't make an array of references. Arrays contain objects. Instances, primitives, and pointers are objects. References aren't.

Question #2, answer elided due to the change in the question.

David Hammen
  • 32,454
  • 9
  • 60
  • 108