reference as data member

would reference as data member be the best choice if the data will not be initialized in constructor?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Boundary;


class Shop
{
    Shop(Boundary& b) : shopBoundary(&b) {}
    Boundary& shopBoundary;
};

class Game
{
public:
    Game() :shop(components["shop"]) {}
private:
    
    std::map<std::string,Boundary> components;// the content is going to be loaded from file
    void load()
    {
         //load components from file
    }

    Shop shop;
};

as you can see, i'd have to make the shopBoundary reference because its value (from component) is going to be loaded after the constructor is executed so in that case i wont be able to intiialize it.

im thinking another way of making a setter for it.

so which one do you think will be the best or is this just a matter of preferences?
Last edited on
Topic archived. No new replies allowed.