Skip to main content
added 231 characters in body
Source Link
Werner Henze
  • 16.8k
  • 12
  • 47
  • 70

As you are using virtual for all base classes you end up with D having only one A (and not one as base of D, one as base of B and one as base of C).

The problem is with your constructors.

The constructor of a derived class calls the constructors of all base classes in the order they are given in the base class list, so in your case D::D calls:

A::A() -> _a and _b uninitialized
B::B(5)
  _a = 5
  _b = 0
C::C(5)
  _a = 0
  _b = 5

It is unclear why you are setting _a and _b multiple times in different constructors. It would be better to initialize members in the constructor's member initializer list and not assign it in a constructor body. You could for example write

A(int a, int b) : _a(a), _b(b) {}
B(int a) : A(a, 0) {}
C(int b) : A(0, b) {}
D() : A(5, 5), B(5), C(5) {}

Here D::D() would result in

A(5, 5) -> _a initialized to 5, _b initialized to 5
B(5) -> no effect (on A/_a/_b)
C(5) -> no effect (on A/_a/_b)

If you want to stick with your design and initialize the variables in the constructor bodies, you can simply add code to D::D to overwrite whatever the base class constructors set:

D() : A(), B(5), C(5) { _a=5; _b=5; }

As you are using virtual for all base classes you end up with D having only one A (and not one as base of D, one as base of B and one as base of C).

The problem is with your constructors.

The constructor of a derived class calls the constructors of all base classes in the order they are given in the base class list, so in your case D::D calls:

A::A() -> _a and _b uninitialized
B::B(5)
  _a = 5
  _b = 0
C::C(5)
  _a = 0
  _b = 5

It is unclear why you are setting _a and _b multiple times in different constructors. It would be better to initialize members in the constructor's member initializer list and not assign it in a constructor body. You could for example write

A(int a, int b) : _a(a), _b(b) {}
B(int a) : A(a, 0) {}
C(int b) : A(0, b) {}
D() : A(5, 5), B(5), C(5) {}

Here D::D() would result in

A(5, 5) -> _a initialized to 5, _b initialized to 5
B(5) -> no effect (on A/_a/_b)
C(5) -> no effect (on A/_a/_b)

As you are using virtual for all base classes you end up with D having only one A (and not one as base of D, one as base of B and one as base of C).

The problem is with your constructors.

The constructor of a derived class calls the constructors of all base classes in the order they are given in the base class list, so in your case D::D calls:

A::A() -> _a and _b uninitialized
B::B(5)
  _a = 5
  _b = 0
C::C(5)
  _a = 0
  _b = 5

It is unclear why you are setting _a and _b multiple times in different constructors. It would be better to initialize members in the constructor's member initializer list and not assign it in a constructor body. You could for example write

A(int a, int b) : _a(a), _b(b) {}
B(int a) : A(a, 0) {}
C(int b) : A(0, b) {}
D() : A(5, 5), B(5), C(5) {}

Here D::D() would result in

A(5, 5) -> _a initialized to 5, _b initialized to 5
B(5) -> no effect (on A/_a/_b)
C(5) -> no effect (on A/_a/_b)

If you want to stick with your design and initialize the variables in the constructor bodies, you can simply add code to D::D to overwrite whatever the base class constructors set:

D() : A(), B(5), C(5) { _a=5; _b=5; }
added 295 characters in body
Source Link
Werner Henze
  • 16.8k
  • 12
  • 47
  • 70

As you are using virtual for all base classes you end up with D having only one A (and not one as base of D, one as base of B and one as base of C).

The problem is with your constructors.

The constructor of a derived class calls the constructors of all base classes in the order they are given in the base class list, so in your case D::D calls:

A::A() -> _a and _b uninitialized
B::B(5)
  _a = 5
  _b = 50
C::C(5)
  _a = 0
  _b = 5

It is unclear why you are setting _a and _b multiple times in different constructors. It would be better to initialize members in the constructor's member initializer list and not assign it in a constructor body. You could for example write

A(int a, int b) : _a(a), _b(b) {}
B(int a) : A(a, 0) {}
C(int b) : A(0, b) {}
D() : A(5, 5), B(5), C(5) {}

Here D::D() would result in

A(5, 5) -> _a initialized to 5, _b initialized to 5
B(5) -> no effect (on A/_a/_b)
C(5) -> no effect (on A/_a/_b)

As you are using virtual for all base classes you end up with D having only one A (and not one as base of D, one as base of B and one as base of C).

The problem is with your constructors.

The constructor of a derived class calls the constructors of all base classes in the order they are given in the base class list, so in your case D::D calls:

A::A() -> _a and _b uninitialized
B::B(5)
  _a = 5
  _b = 5
C::C(5)
  _a = 0
  _b = 5

It is unclear why you are setting _a and _b multiple times in different constructors. It would be better to initialize members in the constructor's member initializer list and not assign it in a constructor body.

As you are using virtual for all base classes you end up with D having only one A (and not one as base of D, one as base of B and one as base of C).

The problem is with your constructors.

The constructor of a derived class calls the constructors of all base classes in the order they are given in the base class list, so in your case D::D calls:

A::A() -> _a and _b uninitialized
B::B(5)
  _a = 5
  _b = 0
C::C(5)
  _a = 0
  _b = 5

It is unclear why you are setting _a and _b multiple times in different constructors. It would be better to initialize members in the constructor's member initializer list and not assign it in a constructor body. You could for example write

A(int a, int b) : _a(a), _b(b) {}
B(int a) : A(a, 0) {}
C(int b) : A(0, b) {}
D() : A(5, 5), B(5), C(5) {}

Here D::D() would result in

A(5, 5) -> _a initialized to 5, _b initialized to 5
B(5) -> no effect (on A/_a/_b)
C(5) -> no effect (on A/_a/_b)
Source Link
Werner Henze
  • 16.8k
  • 12
  • 47
  • 70

As you are using virtual for all base classes you end up with D having only one A (and not one as base of D, one as base of B and one as base of C).

The problem is with your constructors.

The constructor of a derived class calls the constructors of all base classes in the order they are given in the base class list, so in your case D::D calls:

A::A() -> _a and _b uninitialized
B::B(5)
  _a = 5
  _b = 5
C::C(5)
  _a = 0
  _b = 5

It is unclear why you are setting _a and _b multiple times in different constructors. It would be better to initialize members in the constructor's member initializer list and not assign it in a constructor body.