为什么复制构造函数隐藏了C ++中的默认构造函数? [英] Why does copy constructor hide the default constructor in C++?
本文介绍了为什么复制构造函数隐藏了C ++中的默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
int a;
public:
Base(const Base & b)
{
cout<<"inside constructor"<<endl;
}
};
int main()
{
Base b1;
getch();
return 0;
}
这会出现错误。没有匹配的函数调用`Base :: Base()'
为什么?
This gives an error. no matching function for call to `Base::Base()' Why?
推荐答案
默认构造函数只有生成如果你不声明任何构造函数。假设如果你定义自己的构造函数,那么你还可以决定是否需要一个无参数的构造函数,如果这样定义的话。
The default constructor is only generated if you don't declare any constructors. It's assumed that if you're defining a constructor of your own, then you can also decide whether you want a no-args constructor, and if so define that too.
在C ++ 0x,会有一个明确的语法,说你想要的默认构造函数:
In C++0x, there will be an explicit syntax for saying you want the default constructor:
struct Foo {
Foo() = default;
... other constructors ...
};
这篇关于为什么复制构造函数隐藏了C ++中的默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文