实现提供了拷贝构造函数和赋值运算符 [英] Implementation supplied copy constructor and assignment operator

查看:187
本文介绍了实现提供了拷贝构造函数和赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我有一个小小的困惑,其中的实现(编译器)将不提供副本构造函数和副本赋值运算符。 >当我们在类中声明复制ctor和/或复制赋值操作符时。

  2. 有人说当我们从一个有私有复制ctor和/或复制赋值操作符的类派生时。 / li>

我对第二种情况有点困惑,是第二种情况正确。

a)不会为你声明它们,所以你会得到编译时错误。

OR

b)实现将声明和定义它们,但是当编译器定义的实现尝试找到基础类'方法,我们将得到一个编译时错误。



我昨天接受了一个采访,我说(b)这是发生,但访问者不同意,他说(a)。



我试图在Microsoft C / C ++ 14.00和gcc 4.4.5中编译以下代码

  struct A 
{
private:
A& operator =(const A&);
};

struct B:A
{
};


int main()
{
B b1:
B b2;
b1 = b2

return 0;
}

Microsoft编译器输出

  ctor01.cpp(9):错误C2248:'A :: operator =':无法访问在'A'类中声明的私有成员
ctor01.cpp参见声明'A :: operator ='
ctor01.cpp(2):参见声明'A'
这个诊断发生在编译器生成的函数'B& B :: operator = B&)'

gcc编译器输出

  Ctor01.cpp:在成员函数'B& B :: operator =(const B&)':
Ctor01.cpp:4:error:'A& A :: operator =(const A&)'is private
Ctor01.cpp:8:error:在此上下文中
Ctor01.cpp:在函数'int main()'中:
Ctor01 .cpp:15:note:合成方法'B& B :: operator =(const B&)'这里首先需要

声明和定义它,但是当编译器定义的实现试图找到基类方法时,我们将得到一个编译时错误。

解决方案

关于复制构造函数,这是标准所说的(12.8 / 7):


如果类别
的复制构造函数为
隐式定义 has:




  • 类型(或其数组)的非静态数据成员,
    不可访问或不明确的副本
    构造函数,或

  • 具有不可访问或模糊副本的基类
    构造函数


关于副本赋值运算符(12.8 / 12):


如果复制赋值运算符
隐式定义的类
具有:




  • const类型的非静态数据成员,或

  • 参考类型的非静态数据成员,或

  • 非静态数据成员类型(或其数组)与
    不可访问的副本赋值运算符

  • 具有不可访问的副本赋值操作符的基类。 strong>


编译器报告错误的方式,



但是,我相信answer(b)可能更多 正确:基类复制赋值被声明,它是不可访问的。派生类具有隐式的声明的拷贝分配,编译器将尝试定义 如果使用,从而使程序格式不正确。 p>

I have a small confusion regarding the situations where the implementation (compiler) will not supply the copy constructor and the copy assignment operator.

  1. When we declare the copy ctor and/or copy assignment operator in our class.
  2. Some says when we derive from a class which has a private copy ctor and/or copy assignment operator.

I am a little confused about the second situation, is the second situation is precisely.
a) The implementation will not declare them for you, so you will get a compile time error.
OR
b) The implementation will declare and define them, but when the compiler defined implementation tries to find the base class' method, we will get a compile time error.

I had an interview yesterday, I said its (b) that is happening but the interviewer disagrees, he says its (a).

I tried to compile the following code in both Microsoft C/C++ 14.00 and gcc 4.4.5

struct A
{
private:
  A& operator = ( const A& );
};

struct B : A
{
};


int main()
{
  B b1;
  B b2;
  b1 = b2;

  return 0;
}

Microsoft compiler output

ctor01.cpp(9) : error C2248: 'A::operator =' : cannot access private member declared in class 'A'
ctor01.cpp(4) : see declaration of 'A::operator ='
ctor01.cpp(2) : see declaration of 'A'
This diagnostic occurred in the compiler generated function 'B &B::operator =(const B &)'

gcc compiler output

Ctor01.cpp: In member function ‘B& B::operator=(const B&)’:
Ctor01.cpp:4: error: ‘A& A::operator=(const A&)’ is private
Ctor01.cpp:8: error: within this context
Ctor01.cpp: In function ‘int main()’:
Ctor01.cpp:15: note: synthesized method ‘B& B::operator=(const B&)’ first required here 

So I think, the implementation will declare and define it, but when the compiler defined implementation tries to find the base class method, we will get a compile time error. Correct me if I am wrong.

解决方案

Regarding the copy constructor, this is what the standard says (12.8/7) :

A program is illformed if the class for which a copy constructor is implicitly defined has:

  • a nonstatic data member of class type (or array thereof) with an inaccessible or ambiguous copy constructor, or
  • a base class with an inaccessible or ambiguous copy constructor.

Regarding the copy assignment operator (12.8/12) :

A program is illformed if the class for which a copy assignment operator is implicitly defined has:

  • a nonstatic data member of const type, or
  • a nonstatic data member of reference type, or
  • a nonstatic data member of class type (or array thereof) with an inaccessible copy assignment operator, or
  • a base class with an inaccessible copy assignment operator.

How the compiler reports the error, or how it actually falls into it, is pretty much irrelevant from my point of view.

However, I do believe that answer (b) is probably more correct : the base class copy assignment is declared, and it's inaccessible. The derived class has an implicitly declared copy assignment which the compiler will try to define if used, thus making the program ill-formed.

这篇关于实现提供了拷贝构造函数和赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆