Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;关于两类构造函数的问题_C++_Constructor - Fatal编程技术网

C++ C++;关于两类构造函数的问题

C++ C++;关于两类构造函数的问题,c++,constructor,C++,Constructor,我的老师给了我一个示例代码,但我有点困惑 class CBook { ... pubic: CBook(); CBook(string Title, string Auther, int Year); // I know this is the constructor ~CBook(); // and this is the destructor }; 我想知道CBook()的用法,这行代码真的有必要吗 谢谢事实上,如果您这样做,编译器将

我的老师给了我一个示例代码,但我有点困惑

class CBook
{
...
    pubic:
        CBook();
        CBook(string Title, string Auther, int Year); // I know this is the constructor
        ~CBook(); // and this is the destructor
};
我想知道
CBook()的用法,这行代码真的有必要吗


谢谢

事实上,如果您这样做,编译器将调用此构造函数:

CBook obj;
换句话说,您不会向它传递任何参数

因此,如果您删除它并尝试执行以下操作,例如:

CBook obj;
编译器将给您一个类似“无默认构造函数”的错误


有时我们需要一个没有填充任何成员的对象来填充后面的对象。

你应该看一本好书
我知道这是构造函数
不是构造函数而是构造函数(可以有多个)。所以
CBook()
也是一个构造函数。@t.niese好的,可以。谢谢对不起,我没有关于对象的概念。