-2

I know you can definitely have a reference variable in a class, but is there any more "elegant" way to do this than to have a placeholder object? This is my current code (I don't like how this is being done):

Object placeholder = Object();

class MyClass {
private:
    Object& variable = placeholder;
}

EDIT: Reference variables apparently cannot be reassigned during runtime so my example doesn't even work. I would, however, still like to know how to include reference variables in my classes.

  • Do you understand that references cannot be changed to refer to a different object? In what you have `variable` will _always_ refer to `placeholder`. – Fred Larson Mar 01 '22 at 20:37
  • No, I didn't. In that case, this example doesn't even work. Regardless, I still want to know how I can include a reference in a class constructor. Thanks for the information. – TheAngriestCrusader Mar 01 '22 at 20:38
  • I'm not sure what you mean by "include a reference in a class constructor." – Fred Larson Mar 01 '22 at 20:39
  • There is no constructor in this code – Remy Lebeau Mar 01 '22 at 20:40
  • Please be more specific than "include a reference in a class constructor". In particular, what does "include" mean, and why do you want to do it? (Seeing as you haven't understood what references are, the chances that you actually need one are slim.) – molbdnilo Mar 01 '22 at 20:40
  • Do you mean something like [this](https://stackoverflow.com/q/6576109/10077)? – Fred Larson Mar 01 '22 at 20:40
  • My private variable, `variable`, needs to reference an object of type `Object`. – TheAngriestCrusader Mar 01 '22 at 20:41
  • @molbdnilo I have a player object and a home object. The home object is linked to the player through the player's `home` variable, named `variable` in my example. – TheAngriestCrusader Mar 01 '22 at 20:43
  • Do all players have the same home? Can a player's home change? Can more than one player have the same home? Can a player be homeless? Does a home also have players? These are questions that need answering. – molbdnilo Mar 01 '22 at 20:45
  • Players have different homes and the home can change if it gets destroyed (a new home is instantiated if this happens). Players are not homeless at any point. – TheAngriestCrusader Mar 01 '22 at 20:47
  • Please post something one could compile, not just fragments. Also note that find something _elegant_ or _like_ something are personal issues and hard to argue about here. The code you posted does not have even a `class` constructor, so please post some complete code and a description of what you are trying to write. – arfneto Mar 01 '22 at 20:48
  • What you're looking for is some form of pointer (possibly, but not necessarily, a "smart" one). Read about them in your favourite C++ book. – molbdnilo Mar 01 '22 at 20:51
  • @arfneto Clearly I misunderstood what a constructor is, I assumed that everything I put in the class was in my constructor... I'll have to look into this later on. – TheAngriestCrusader Mar 01 '22 at 20:52
  • @molbdnilo smart pointers sound like a good thing to look into. I don't currently have a C++ book (started learning C++ not too long ago). – TheAngriestCrusader Mar 01 '22 at 20:53
  • You can't learn C++ without a book. There is a list of good ones [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) . (There are astonishingly few good introductory C++ books, considering its mature age. Even the good ones are far from great..) – molbdnilo Mar 01 '22 at 20:54
  • @molbdnilo A tour of C++ looks pretty good (well, as good as I'm gonna really get, I guess), might have to invest in it when I have a bit more money. Thanks for the link! – TheAngriestCrusader Mar 01 '22 at 20:57
  • _Players have different homes and the home can change_ Then you can't use a reference, since these can't be (re-)assigned, only initalised when they first come into existence. @molbdnilo has put you on the right track. – Paul Sanders Mar 01 '22 at 21:14
  • Post some code so one can compile and maybe show you an answer with an example closer to what you need – arfneto Mar 01 '22 at 22:08
  • A good rule of thumb is never have a const or reference member variable unless you understand the limits they impose in detail. – doug Mar 01 '22 at 22:49

1 Answers1

1

I hope this link helps: Reference variable with error, must be initialized in constructor base/member initializer

It doesn't directly tell you if there is an elegant way but it expands more on the approaches that might lead to an error, therefore, you might want to consider going with the basic approach mentioned in this link!

juhikushwah
  • 33
  • 1
  • 8