1
class Inner {
public:
  Inner() {
    std::cout << "Inner" << std::endl;
  }
};

class Outer {
public:
  Outer() {
    i = Inner();
  }

  Inner i;
};

int main() {
  Outer o;
}

The above code prints Inner twice, which indicates that in Outer, Inner i; was first initialized once, and then in its constructor, it's initialized again. If Inner is a rather expensive class to initialize, this seems very inefficient. What's a good way to avoid such double-initialization?

OneZero
  • 11,556
  • 15
  • 55
  • 92

0 Answers0