What will be printed out as a result of the following code execution?

#include <iostream>

using namespace std;

struct A {
  A() { cout << "A()"; }
  A(int) { cout << "A(int)"; }
  void operator=(int) { cout << "="; }
};

struct B {
  A a, b;
  B() : b(1) { a = 2; }
};

int main() {
  B b; 
}
Explanation
Members of the class are initialized in the order in which they are declared before entering the constructor body. It means that when we create a class B, B::a will be initialized first using the A::A() constructor and B::b will be initialized then using the A::A(int) constructor, and then the operator= will be called in the body of B::() constructor.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes