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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测