【C++】构造函数调用规则

        欢迎来到博主 Apeiron 的博客,祝您旅程愉快 !时止则止,时行则行。动静不失其时,其道光明。


1、缘起

(1)默认情况下,C++ 编译器至少给一个类添加 3 个函数

        ①  默认构造函数(无参,函数体为空)

        ②  默认析构函数(无参,函数体为空)

        ③  默认拷贝构造函数,对属性进行值拷贝

(2)构造函数调用规则如下:

        ①  如果用户定义有参构造,C++ 不在提供默认无参构造,但是会提供默认拷贝构造

        ②  如果用户定义拷贝构造,C++ 不会再提供其他 构造函数(有参构造和无参构造)


2、示例代码

①  如果用户定义有参构造,C++ 不在提供默认无参构造,但是会提供默认拷贝构造

示例代码 1 :

#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
    Person()
    {
        cout << "Person 默认构造函数的调用" << endl;
    }

    Person(int age)
    {
        cout << "Person 有参构造函数的调用" << endl;
        p_age = age;
        cout << endl;
    }

    ~Person()
    {
        cout << "Person 析构函数的调用" << endl;
    }

    Person(const Person& p)
    {
        cout << "Person 拷贝构造函数的调用" << endl;
        p_age = p.p_age;
        cout << endl;
    }

    int p_age = 0;
};


void test()
{
    Person p1(7);
    Person p2(p1);

    cout << "p2 的年龄:" << p2.p_age << endl;
    cout << endl;
}
int main()
{
    test();

    cout << endl;
    system("pause");
    return 0;
}

        上述代码中写了拷贝构造函数,在运行这行 Person p2(p1); 程序时,编译器会自动调用用户自定义的拷贝构造函数。那么,如果用户不自定义拷贝构造函数,编译器会执行怎么样的操作呢?请看示例代码 2。


示例代码 2 :

#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
    Person()
    {
        cout << "Person 默认构造函数的调用" << endl;
    }

    Person(int age)
    {
        cout << "Person 有参构造函数的调用" << endl;
        p_age = age;
        cout << endl;
    }

    ~Person()
    {
        cout << "Person 析构函数的调用" << endl;
    }

    int p_age = 0;
};


void test()
{
    Person p1(7);
    Person p2(p1);

    cout << "p2 的年龄:" << p2.p_age << endl;
    cout << endl;
}
int main()
{
    test();

    cout << endl;
    system("pause");
    return 0;
}

上述代码中没有提供拷贝构造函数,但是编译器提供了一个默认的拷贝构造函数。在运行这行 Person p2(p1); 程序时,编译自动值拷贝 p1 的属性,然后将其赋值给 p2。 


示例代码 3:

#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
    Person(int age)
    {
        cout << "Person 有参构造函数的调用" << endl;
        p_age = age;
        cout << endl;
    }

    ~Person()
    {
        cout << "Person 析构函数的调用" << endl;
    }

    Person(const Person& p)
    {
        cout << "Person 拷贝构造函数的调用" << endl;
        p_age = p.p_age;
        cout << endl;
    }

    int p_age = 0;
};


void test()
{
    Person p;
}
int main()
{
    test();

    cout << endl;
    system("pause");
    return 0;
}

        上述代码中用户自定义了有参构造函数,没有自定义默认构造函数。如果用户定义有参构造,编译器不在提供默认无参构造。此时,编译器出现报错。 


②  如果用户定义拷贝构造,C++ 不会再提供其他构造函数 

示例代码:

#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
    Person(const Person& p)
    {
        cout << "Person 拷贝构造函数的调用" << endl;
        p_age = p.p_age;
        cout << endl;
    }

    int p_age = 0;
};


void test()
{
    Person p1(7);
    Person p2(p1);

    cout << "p2 的年龄:" << p2.p_age << endl;
    cout << endl;
}
int main()
{
    test();

    cout << endl;
    system("pause");
    return 0;
}

上述代码中只写了拷贝构造函数,编译器就给出了报错,这是因为如果用户定义拷贝构造,编译器不会再提供其他构造函数。 


3、总结

        本期的分享总结就到这里了,如果有疑问的小伙伴,我们在评论区交流嗷~~~,笔者必回,我们下期再见啦 !

博客中难免存在疏漏和错误之处,皆归因于作者水平有限,诚请各位读者不吝指正 !

< C++ >  专栏系列持续更新 ,欢迎订阅关注 !

猜你喜欢

转载自blog.csdn.net/qq_51870334/article/details/130818866