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.

levzettelin
  • 2,600
  • 19
  • 32

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
  • 36,443
  • 4
  • 48
  • 76