//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.
//A.hclass A{ A(B& b) : b(b) {} B& b;}
//B.hclass B{ B() : a(*this) {} A a;}
Quote:Original post by iMalcQuote:Original post by CodekaThe 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.
Remove the default constructor.
Edit: too slow!
struct Foo { int &r Foo () : r(r) {} };
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. [...] ]
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.
struct Foo { char &c };
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.
[...]
Quote:
But it is not:
vec1.push_back(vec2);
int main(){ int a; int& b= a; int& & c = b;}
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.
int &b=a;
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).Quote:A reference shall be initialized to refer to a valid object or function
struct Foo { int &p Foo () : p(p){} };
p(p)
int a = a;
Foo & default_instance() { static Foo instance; return instance;};struct Bar { Bar(Foo & ref = default_instance());};