Home  >  Q&A  >  body text

c++11 - 这段 C++ 代码究竟创建了多少个对象?

class stu{
public:
    int age;
    int score;
    stu() = default;
    stu(int a, int s):
        age(a), score(s) { cout << " i am constructor" << endl;}
    stu(const stu& stu1):
        age(stu1.age), score(stu1.score) { cout << " i am copy constructor" << endl;}
};
const stu combine(const stu &stu1, const stu &stu2)
{
    stu new_stu(stu1.age + stu2.age, stu1.score + stu2.score);
    return  new_stu;
}
int main()
{
    stu stu1(22,100);
    stu stu2(23,99);
    stu result(combine(stu1, stu2));
    cout << result.age << result.score << endl;
}

我的理解是应该创建了四个对象,其中一个是临时对象,由combine(stu1, stu2)函数返回时创建。类的构造函数执行打印一行字符串的功能,通过有多少行字符串可以推出执行了多少次构造函数,代码在运行时只有三行字符串,意味着只创建了三个对象,究竟哪里有问题??

巴扎黑巴扎黑2714 days ago844

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:07:32

    1. First of all, there is a correction. There is nothing wrong with returning a temporary object from combine, but the copy constructor is called here, not the constructor. Then the constructor is called again when result is declared, So the final number is two constructors + one copy constructor + one constructor

    2. Then the question arises, why is the copy constructor not reflected at all? This is because the compiler will skip the copy constructor and directly execute the constructor, so the result becomes three times. Constructor, for related content, please refer to c++ primerChapter 13.

    Although the compiler will skip the copy constructor, you must ensure that the copy constructor of the class is accessible (not private). If you verify this, an error will be reported.

    class stu{
    public:
        int age;
        int score;
        stu() = default;
        stu(int a, int s):
            age(a), score(s) { cout << " i am constructor" << endl;}
    prvate:
        //虽然编译器绕过了拷贝构造函数,但是仍然需要拷贝构造函数可见.这里会编译错误
        stu(const stu& stu1):
            age(stu1.age), score(stu1.score) { cout << " i am copy constructor" << endl;}
    };

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:07:32

    This is RVO (Return Value Optimization)

    Reference: https://en.wikipedia.org/wiki/Return_value_optimization

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:07:32

    Five objects were generated
    stu1 +1
    stu2 +1
    new_stu +1
    const stu +1
    result +1

    The compiler will enable return value optimization by default
    You can turn off this optimization by adding the -fno-elide-constructors option when compiling with gcc.
    The result of running the compiled program is as follows:
    i am constructor
    i am constructor
    i am constructor
    i am copy constructor
    i am copy constructor
    45199

    reply
    0
  • Cancelreply