How do you initialize a reference type in default constructor?

Started by
16 comments, last by Antheus 14 years, 5 months ago
Then you could do (I think this you want you wanted - B contains a A, while A has the reference back to B:

//A.hclass A{   A(B& b) : b(b) {}   B& b;}

//B.hclass B{   B() : a(*this) {}   A a;}


But be careful with what you do with that reference that comes from B's this pointer in A's constructor.
Advertisement
Quote:Original post by iMalc
Quote:Original post by Codeka
Remove the default constructor.

Edit: too slow!
The default constructor can still be used but the only things you could initialise the reference with would be a global or static variable, or new object, or an object obtained through some other function call.

I just looked up the Holy Standard, because I thought the following would be legal:
    struct Foo {            int &r            Foo () : r(r) {}    };

But it is not:
Quote:ISO/IEC 14882:2003(E), 8.3.2.4
There shall be no references to references [...]A reference shall be initialized to refer to a valid object
or function. [Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior. [...] ]


So forget what I did not recommend :S


Quote:However the first two of those would defeat the point of having it as a reference, the third would give you unique instances rather than shared ones, and the fourth doesn't really give any bennefits either. So yeah, remove the default constructor, or reconsider pointers instead.

It is interesting to note that if you put a reference into a class definition ...

    struct Foo {            char &c    };


... then this perfectly prevents the compiler from generating a default constructor behind the back:

Quote:12.1
[...]
5 - A default constructor for a class X is a constructor of class X that can be called without an argument.
[...]
7 - Before the implicitly-declared default constructor for a class is implicitly
defined, all the implicitly-declared default constructors for its base classes and its nonstatic data members shall have been implicitly defined.
[...]



And as initializing a const or a reference member with an empty argument list made your program ill-formed, the compiler won't derive it implicitly. Same argumentation for copy assigment (T& operator = (T const &)).
Thanks to all...

After spending a few hours reading through the reference and testing some stuff. I belive a pointer is what I am looking for. I have added a check for self assignment so thanks for the heads up on that one. And thanks to anyone else who helped with the learning.

Regards

Chad
Quote:
But it is not:


I very much doubt that your interpretation is correct, or all my understanding of C++ just broke apart and all my code turned out to be ill-formed. Haven't you done things like:

vec1.push_back(vec2); 


I suppose this means that something like the following doesn't compile:

int main(){    int a;    int& b= a;    int& & c = b;}


If you remove the second & from the declaration of c, it will be OK, and c will be a reference to the same object as b, namely a.
Quote:Original post by visitor
I suppose this means that something like the following doesn't compile:

int main(){    int a;    int& b= a;    int& & c = b;}


If you remove the second & from the declaration of c, it will be OK, and c will be a reference to the same object as b, namely a.


I guess this is about "reference to reference", which is indeed not allowed. The line
    int &b=a;

reads more like "dereference the rhs, and build a reference from it", which is why
    int main () {            int a, &b = a, &c = b;    }
is a well-formed program, and from that standpoint the reason why you cannot build a reference from a temporary (because a temporary has no address).


But my point was more about
Quote:A reference shall be initialized to refer to a valid object or function

and as in
    struct Foo { int &p Foo () : p(p){} };

or more precise
    p(p)

p is not a readily constructed entity (neither as a reference, nor as an value) and hence an invalid object, and thus not de-referenceable, that initialization is ill-formed.
Missed that you were in fact demonstrating an attempt to initialize a reference to basically nothing. Yep, initializing anything with itself doesn't do any good.

int a = a;


Still uninitialized.
Does it really make sense to construct a Graphics object without an Application? Conceptually it doesn't make much sense to me, so I would wager that your default constructor just doesn't belong.

Foo & default_instance() {  static Foo instance;  return instance;};struct Bar {  Bar(Foo & ref = default_instance());};

This topic is closed to new replies.

Advertisement