I think we have all misunderstood the OP's problem here, and that includes me. OK, he has failed to initialise object
, but, if I read this right, that's not what he's asking about. He wants to know, I believe, how a reference can 'point to' an invalid object.
Well, normally it can't. The contract, when return a reference, is that it will always point to something valid, and specifically will not, under the covers, contain nullptr
. That's why you return a reference, and not a pointer. Because it carries that guarantee.
So, has @Ralff painted himself into a corner? Well, not quite. The usual solution of course would just be to have get_object()
return a pointer in the first place, but he evidently doesn't want to do that.
A good solution here is to keep an object of type Object
around that serves as a placeholder for an invalid object. Then it's easy. I'll stop waffling and post some code:
#include <iostream>
class Object
{
// ...
public:
static Object invalid_object;
bool is_valid () const { return this != &invalid_object; };
};
Object Object::invalid_object;
class Foo
{
private:
Object * object = nullptr;
public:
Foo() { }
virtual const Object & get_object() const { return (object) ? *object : Object::invalid_object; }
};
And now you can do:
int main ()
{
Foo foo_obj;
const Object& obj = foo_obj.get_object ();
if (obj.is_valid ())
std::cout << "object is valid" << std::endl;
else
std::cout << "object is invalid" << std::endl;
return 0;
}
Live demo.
OP, for other approaches, check out also std::optional, or consider throwing an exception from get_object()
(although that would not be my choice).