bosthhe1 / cpushpush

0 stars 0 forks source link

=delete和=default #44

Open bosthhe1 opened 1 year ago

bosthhe1 commented 1 year ago

由于我们写了构造函数,编译器就不会自动生成默认的构造函数=default就是告诉编译器默认生成构造函数

class A
{
private:
    int a;
public:
    A(int _a)
    {
        a = _a;
        std::cout << "构造函数" << std::endl;
    }
    A() = default;
};
int main()
{
    A a;
    A b(1);
    return 0;
}

image

bosthhe1 commented 1 year ago

detele是为了表明不调用该函数

class A
{
private:
    int a;
public:
    A(int _a)
    {
        a = _a;
        std::cout << "构造函数" << std::endl;
    }
    A() = delete;//不希望调用不带的构造函数
};
int main()
{
    A a;
    A b(1);
    return 0;
}

image