C++ 有没有办法知道该语句将调用哪个构造函数;学生s3=func(s1,s4)“;?

C++ 有没有办法知道该语句将调用哪个构造函数;学生s3=func(s1,s4)“;?,c++,C++,我想要一个不会复制elison的编译器(在线/离线)。我想看看我们在“c++的构造函数和析构函数”理论中学到的实际输出 语句“Student s3=func(s1,s4)”将调用哪个构造函数。此语句未调用任何复制构造函数、参数化构造函数和重载赋值运算符。那么,这个对象是如何构造的呢 我也使用此编译器进行了测试: #包括 #包括 使用名称空间std; 班级学生 { 字符*名称; int-id; 公众: 学生(字符*n,整数i) { name=新字符[strlen(n)+1]; strcpy(名称,

我想要一个不会复制elison的编译器(在线/离线)。我想看看我们在“c++的构造函数和析构函数”理论中学到的实际输出

语句“Student s3=func(s1,s4)”将调用哪个构造函数。此语句未调用任何复制构造函数、参数化构造函数和重载赋值运算符。那么,这个对象是如何构造的呢

我也使用此编译器进行了测试:

#包括
#包括
使用名称空间std;
班级学生
{
字符*名称;
int-id;
公众:
学生(字符*n,整数i)
{
name=新字符[strlen(n)+1];
strcpy(名称,n);
id=i;
coutGCC(无论是否在线托管)具有:

-fno elide构造函数

<> P> C++标准允许一个实现省略创建一个临时的,它只用于初始化同一类型的另一个对象。指定这个选项禁用优化,并强制G++在所有情况下调用复制构造函数。这个选项也使G++调用琐碎的成员函数,否则这些函数将被扩展到内联。

在C++17中,编译器需要省略这些临时变量,但此选项仍会影响普通成员函数


我想你弄错了。复制构造函数的名称很清楚

为了更清楚地说明这一点,请将您的主要函数重新写入:

intmain()
{
学生s1(“abcd”,16岁);
学生s4(“wxyz”,17岁);

cout Copy elision是语言的一部分。我们在“c++的构造函数和析构函数”的理论中学习了这一点“。你的课程听起来像是教了一个不准确但可能仍然有用的模型。@Caleth我真的很抱歉。我不是那个意思。我的意思是,我想看到没有复制省略的输出。谢谢。非常感谢。这真是太好了。如何添加选项”-fno elide构造函数”在代码块中?我不知道代码块,但请尝试:
#include<cstring>
#include<iostream>
using namespace std;
class Student
{
    char* name;
    int id;
public:
    Student(char* n, int i)
    {
        name= new char[strlen(n)+1];
        strcpy(name, n);
        id=i;
        cout<<"Constructor "<<name<<endl;
    }
    Student(const Student& s)
    {
        name= new char[strlen(s.name)+1];
        strcpy(name, s.name);
        id=s.id;
        cout<<"Copy constructor "<<name<<endl;
    }
    void operator = (const Student &s )
    {
        name= new char[strlen(s.name)+1];
        strcpy(name, s.name);
        id=s.id;
        cout<<"Assignment Operator "<<name<<endl;
    }
    ~Student()
    {
        cout<<"Destructing "<<name<<endl;
        delete[] name;
    }
};
Student func(Student s, Student t)
{
    return s;
}
int main()
{
    Student s1("abcd", 16);
    Student s4("wxyz", 17);
    Student s3=func(s1, s4);
}
Constructor abcd
Constructor wxyz
initializing s3
Copy constructor wxyz // for the second parameter of the function
Copy constructor abcd // for the first parameter of the function
Copy constructor abcd // for s3
Destructing abcd // for the first parameter of the function
Destructing wxyz // for the second parameter of the function
done
Destructing abcd
Destructing wxyz
Destructing abcd
Constructor abcd
Constructor wxyz
initializing s3
Copy constructor abcd
done
Destructing abcd
Destructing wxyz
Destructing abcd
Constructor abcd
Constructor wxyz
initializing s3
Copy constructor abcd // for the return value of the function
Copy constructor abcd // for s3
Destructing abcd // for the return value of the function
done
Destructing abcd
Destructing wxyz
Destructing abcd