C++ Grand Parent 默认构造函数调用

标签 c++ clang

只要包含 Rect 默认构造函数,我就有以下代码可以正常运行。但是,如果我将其注释掉,希望它会“跳到”Shape 默认构造函数,但它无法编译。

#include <cstdio>

class Shape
{
public:
    Shape()
    {
        printf("Shape default called\n");
    }
};

class Rect : public Shape
{
public:
    int width;
    int height;
    
    Rect()
    {
        printf("Rect default called\n");
    }

    Rect(int width, int height)
    {
        this->width = width;
        this->height = height;
    }

    int area()
    {
        return width*height; 
    }
};

class Square : public Rect
{
public:
    Square(int width)
    {   
        printf("Square constructor called.\n");
        this->width = width;
        this->height = width;
    }
};

int main(int argv, char** argc)
{
    printf("...\n");
    Square r = Square(10);
    printf("Area: %d\n", r.area());
    return 0;
}

当程序运行并编译时,我得到以下输出

...

Shape default called

Rect default called

Square constructor called.

Area: 100

我希望简单地删除 Rect 默认构造函数并得到

...

Shape default called

Square constructor called.

Area: 100

因为所有 rect 默认构造函数将做的就是坐在那里满足 clang 的错误。它无意做任何额外的事情。有办法实现吗?

最佳答案

您应该使用成员初始值设定项列表语法来使用您的参数调用适当的 Rect 构造函数:

Square(int width) : Rect(width, width)
{
    printf("Square constructor called.\n");
}

请注意,您不需要手动分配给 this->widththis->heightRect 构造函数会执行此操作给你。

在构造函数体中使用初始化列表而不是赋值的主要优点是,它可以避免对默认构造函数进行不必要的调用。参见 this answer详细解释。

它还允许您继承(并拥有)没有默认构造函数的类的数据成员。

如果您需要先对值做一些事情,那么在构造函数体中赋值是完全可以的,但是您的基类(或成员类,如果您正在初始化数据成员)必须有一个默认构造函数。

这样做的原因是每个数据成员的构造函数都必须(并且已经)在类的构造函数体开始执行之前被调用。初始化列表允许您根据您提供的参数选择调用哪个构造函数。如果您不在初始化列表中初始化数据成员,它将使用其默认构造函数构造(这就是必须声明它的原因),然后才能在构造函数主体中为其分配任何值。

关于C++ Grand Parent 默认构造函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897864/

相关文章:

c++ - C++ 中的函数 const 局部对象会怎样?

c++ - 在循环中递增日期

c++ - Rcpp_模块 : exposing class method with formal R arguments

ios - 为 iOS 7 编译 x264

c++ - std::function::operator bool 应该在 move 后返回 false 吗?

c++ - ADL 应该如何发挥作用?

c++ - Vim 无法通过 clang 设置 omnifunction

c++ - 类类型错误 C++ with struct

c++ - 在 Ubuntu 上使用 libc++ 的 clang 编译器出现问题

c++ - 窗口系统调用