c++ - Inheriting constructors w / wo their default arguments? -
c++ primer (5th edition) on page 629 states:
- if base class constructor has default arguments, arguments not inherited.
i tried myself , me seems derived constructor generated compiler has same default arguments base constructor.
here's little test:
#include <iostream> struct base { base() = default; base(int x_, int y_ = 88, int z_ = 99) : x(x_), y(y_), z(z_) {} virtual void debug() const { std::cout << "\nx - " << x << ", y - " << y << ", z - " << z << '\n'; } private: int x, y, z; }; struct derived : base { using base::base; }; int main() { base b(1); b.debug(); // x - 1, y - 88, z - 99 derived d(5); d.debug(); // x - 5, y - 88, z - 99 return 0; }
( can run here - http://coliru.stacked-crooked.com/a/26cbb85757c1f021 )
so inheriting default arguments inherited constructor or not?
if not, how come i'm not getting junk last 2 members same exact values default argumuments constructor inherited base?
searched on internet clear response found none.
from [class.inhctor]:
the candidate set of inherited constructors class x named in using-declaration consists of actual constructors , notional constructors result transformation of defaulted parameters , ellipsis parameter specifications follows:
— [...]
— each non-template constructor of x has @ least 1 parameter default argument, set of constructors results omitting ellipsis parameter specification , successively omitting parameters default argument end of parameter-type-list, and
— [...]
we have 2 non-template constructors of base
. default constructor of base
introduces candidate:
derived() : base() { }
and other constructor of base
introduces 1 candidate each successively omitted parameter. namely:
derived(int x, int y, int z) : base(x, y, z) { } derived(int x, int y) : base(x, y) { } derived(int x) : base(x) { }
the default arguments not inherited - different constructor each number of arguments. derived(5)
calls base(5)
, not base(5, 88, 99)
.
the end result same - how there bit different.
Comments
Post a Comment