3

Is this code legal?:

class BaseClass
{
  public:
    BaseClass (int *p) : p_ (p) { }

  private:
    int *p_;
};

class SubClass : public BaseClass
{
  public:
    SubClass () : BaseClass (&i_), i_ (123) {}

  private:
    int i_;
};

It is well-known that the base-class gets constructed before the members of the sub-class, which is why I'm wondering.

Toby Brull
  • 2,445
  • 17
  • 29

1 Answers1

1

Yes, this is fine: while the lifetime of (the relevant instance of) SubClass::i has yet to begin, its storage exists, and a pointer to it may be formed (though not used for much yet).

Davis Herring
  • 30,864
  • 3
  • 33
  • 65