I completely get what the OP is getting at.
When references were introduced it was decided to prevent a reference from existing without actually referring to anything. This itself makes perfect sense and is one of the useful things that sets it apart from pointers.
However, the language could have also been designed such that if a reference is ever declared and NOT made to refer to anything, that it actually refers to a temporary default-constructed object. This also of course cannot subsequently be reassigned, no change there.
Assuming you follow so far, it almost seems like a plausible idea. Okay, now think about this: How does declaring one of these references to a default-constructed object differ from declaring an ordinary object? I'd say that the only difference is whether the underlying object is stored on the heap, or as part of it's parent classes memory layout. Feels a bit C# class vs struct-ish to me.
So would this actually be useful? Not really. In the majority of cases storing the object as part of it's parent is going to be more efficient. The only case where you wouldn't want to do that is when the object is large and it's parent is on the stack. In that once case I think it is acceptable to force the programmer to have to go that extra mile (or millimeter) to use a smart pointer, instead. Hence my conclusion of why nobody wants the discussed feature.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Then you'd have one rule for objects with a default constructor and another for those without. Not much of an improvement there.However, the language could have also been designed such that if a reference is ever declared and NOT made to refer to anything, that it actually refers to a temporary default-constructed object.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Or a VB-esque IsNothing() function.....
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Practical and possible do not always see eye to eye in this cruel world of ours.
i'm completely lost here....what's the big deal?
Foo* foo = new Foo;
Foo& bar = *foo;
bar.WeeICanUsePeriodNow();
Cause this is cooler
Code:#include <iostream> class CAwesome{ public: void DoAmesomePrint() { std::cout<<"Awesome!!!!!!!!!!"<<std::endl; } }; int main() { CAwesome &awesome = *(new CAwesome); awesome.DoAmesomePrint(); std::cin.get(); delete &awesome; return 0; }
Woop?
Since this doesn't break as much. This is even cooler.
Code:#include <iostream> #include <new> class CAwesome{ public: void DoAmesomePrint() { std::cout<<"Awesome!!!!!!!!!!"<<std::endl; } }; int main() { try { CAwesome &awesome = *(new CAwesome); awesome.DoAmesomePrint(); std::cin.get(); delete &awesome; return 0; } catch(std::bad_alloc e) { std::cerr << e.what() << std::endl; return -1; } }