开发者

Is calling base class constructor always necessary in C++?

Suppose I have some class C, and I inherit from it and name this class D. Do I always have to call C's default constructor as in this example:

class C {
    public:
        C() { ... }
};

class D {
    public:
        D() : C() { ... }
};

Note that C has only the default cons开发者_运维百科tructor. Do I have to call it from D? I couldn't figure out how to find this out.

Thanks, Boda Cydo.


You don't need to specify the base class constructor in your derived type constructor's initializer list. When it is omitted an attempt will be made to call the base constructor with no parameters. If no such parameterless base constructor exists, then you'll get a compiling error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜