Const makes all the difference. Why?

class T{int i; public: T(int i):i(i){}};

int main(){
T& a1(0); //C2440
const T& a2(0);
}
Last edited on
"_"
ur problem is?
ur question is?
what does that piece of code do?

"_"
Defined are two references a1 and a2. The difference between a1 and a2 is only const. Without const in main error C2440 is pulled. Why?
Last edited on
closed account (DSLq5Di1)
http://stackoverflow.com/questions/2088259/literal-initialization-for-const-references
Const is unchangable...
Using the constructor
T::T(int i)
via the call
a1(0)
an object of type T or const T was created.
The literal was copied via the copy constructor of build-in type int into the data-member i.
The const or non-const reference a1 or a2 can then be initialized with this newly created non-literal object.
Still cant see why T& a1(0) is not working.
Last edited on
Somebody?

Last edited on
Anybody?
Still cant see why T& a1(0) is not working.


A T& by itself is not an object. It merely references another object by a different name. In order to have a T&, you must also have a T (ie: a non-reference object).

Consider this:

1
2
T obj(0);  // OK
T& ref(obj); // OK 


Here, 'ref' and 'obj' are different names for the same object: 'obj'. If you change 'ref', you are also changing 'obj'.

With that in mind... think about what you're trying to do here:

 
T& a1(0);


This code makes no sense. To what object is a1 referring to? There's no T here. That's why the compiler complains.
Topic archived. No new replies allowed.