cool-warm-self-know 2019-07-08 21:09 采纳率: 0%
浏览 409
已结题

c++11中 构造函数 与 复制构造函数和赋值操作符的关系 ???

各位好,以下代码:

#include<iostream>
using namespace std;

class B {
public:
  B() {}
  virtual ~B() {}
  virtual void paly() = 0;
private:
 B(const B&) = delete;
 B& operator=(const B&) = delete;
};

class D: public B {
public: 
 explicit D(int a) {}
 void paly() {cout << "qwertyuiop" <<endl;}
};

int main() {
int a = 0;
B* bp = new D(a);

bp->paly();
delete bp;
return 0;
}

使用 g++ -Wall -std=c++11 -o main a.cc 编译没有问题;
但是注释掉B() {} 构造函数后出现错误:代码如下:

#include<iostream>
using namespace std;

class B {
public:
  ///////B() {}
  virtual ~B() {}
  virtual void paly() = 0;
private:
 B(const B&) = delete;
 B& operator=(const B&) = delete;
};

class D: public B {
public: 
 explicit D(int a) {}
 void paly() {cout << "qwertyuiop" <<endl;}
};

int main() {
int a = 0;
B* bp = new D(a);

bp->paly();
delete bp;
return 0;
}

g++报错如下:

a.cc: In constructor ‘D::D(int)’:
a.cc:16:20: error: no matching function for call to ‘B::B()’
  explicit D(int a) {}
                    ^
a.cc:10:2: note: candidate: B::B(const B&) <deleted>
  B(const B&) = delete;
  ^
a.cc:10:2: note:   candidate expects 1 argument, 0 provided

如果 把delete取消,代码如下: 又可以成功编译。

#include<iostream>
using namespace std;

class B {
public:
  ///////B() {}
  virtual ~B() {}
  virtual void paly() = 0;
private:
 ////B(const B&) = delete;
 ////B& operator=(const B&) = delete;
};

class D: public B {
public: 
 explicit D(int a) {}
 void paly() {cout << "qwertyuiop" <<endl;}
};

int main() {
int a = 0;
B* bp = new D(a);

bp->paly();
delete bp;
return 0;
}

所以我想问, 对于复制构造和赋值操作符的禁用 是会 影响 默认函数的合成吗?
c++11里面有相关的描述吗???
谢谢。

  • 写回答

2条回答

  • zqbnqsdsmd 2019-07-08 22:20
    关注
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败