4

Best explained with an example:

Class banana {
    int &yumminess;
    banana::banana() {
        //load up a memory mapped file, create a view
        //the yumminess value is the first thing in the view so
        yumminess = *((int*)view);
    }
}

But that doesn't work :/ there is no way I can know where the view is going to be when I dreclare the "yumminess" reference variable. Right now i just use a pointer and dereference it all the time, is there any way to bring this little extra bit of convenience to my class?

Jake Freelander
  • 1,471
  • 1
  • 19
  • 26

2 Answers2

6

In short: No, it's intentionally not possible.

Think twice: Something like uninitialized references cannot really exist; such wouldn't make sense at all.
Thus they'll need to be set at the time of construction of the enclosing class, or at a point of static initialization.

You'll need to use pointers for such case.


Besides note that

 yumminess = (int*)view;

would be wrongly casted (to a pointer) anyway.


"Right now i just use a pointer and dereference it all the time ..."

That's also easy to overcome writing an appropriate member function to access the reference.

int* yumminess;

// ...

int& yumminessRef() {
    if(!yumminess) { 
        throw some_appropriate_exception("`yumminess` not initialized properly.");
    }
    return *yumminess;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • That's an entirely _inappropriate_ getter [member function]. – Lightness Races in Orbit Jul 07 '14 at 16:37
  • yea just noticed that. I think I got an idea though, I'll try make another class "convenientBanana" that inherits the regular banana and gets the addresses necessary for ref initialization from there. – Jake Freelander Jul 07 '14 at 16:37
  • @LightnessRacesinOrbit Yes in sense of a _getter_ method that was wrong. Reworded a bit. – πάντα ῥεῖ Jul 07 '14 at 16:39
  • I still wish you'd stop misusing the word "method". – Lightness Races in Orbit Jul 07 '14 at 16:44
  • @LightnessRacesinOrbit Sorry, I was taught so over 25 years ago, and still tend to use the term (what you've once learned). I think they used the term, to not confuse us with what we already were knowing about c functions at that time. C++ and OOP was all pretty new those days ;) ... – πάντα ῥεῖ Jul 07 '14 at 16:56
  • @πάνταῥεῖ Well so was I and so were many of us; doesn't mean you have to keep repeating the wrong stuff and teaching it to the next generation. :) – Lightness Races in Orbit Jul 07 '14 at 17:06
  • @LightnessRacesinOrbit I'll try to keep my wording cleaner in the future, THX for helping improving myself. – πάντα ῥεῖ Jul 07 '14 at 17:53
  • @KarliRaudsepp Interestingly enough, 2 of my colleagues asked me about almost the same thing today. There are ways, to initialize references from inherited classes of course. You can pass a reference to a member instantiated in an inherited class to a base class constructor in the member initializer list. But to clarify about your obstacles doing this in particular, I'd recommend to ask another question. – πάντα ῥεῖ Jul 07 '14 at 17:59
2

No, not directly.

If you think the pointer is inconvenient, have a look at std::optional.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62