C++ - autogenerated copy constructor and assignment operator gotchas_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > C++ - autogenerated copy constructor and assignment operator gotchas

C++ - autogenerated copy constructor and assignment operator gotchas

 2013/1/25 11:05:58  joe.bq.wang  程序员俱乐部  我要评论(0)
  • 摘要:Ithasbeenchangedthatthesemanticofthecopyconstructorandtheassignmentoperatorintherelationshipwithclasshierarhcyfromvs10tovs12...onecaseofthegotchasislikethis:#include<iostream>structA{A&operator=(constA&){return*this;}template<
  • 标签:has c++ CTO Opera

It has been changed that the semantic of the copy constructor and the assignment operator in the relationship with class hierarhcy from vs10 to vs12...
one case of the gotchas is like this:

#include <iostream>
struct A
{
  A& operator=(const A&) { return *this; }
  template <typename T> A& operator=(const T&) // while one can argue that this is the desired semantic, B will continue to search its parent's definition if there is none provided 
  {
    std::cout << "This should not be printed" << std::endl;
    return *this;
  }
};

struct B : A { };



int _tmain(int argc, _TCHAR* argv[])
{
	  B x;
  B y;
  x = y;
 return 0;
}

?

?

You will see the output "This should not be printed" printed. but if you run the same code in VS 2012, you won't see it.?

if you change the main to this:

int _tmain(int argc, _TCHAR* argv[])
{
	 // B x;
  //B y;
  //x = y;

  A x ;
  A y ;
  y = x;
 return 0;

you will NOT see the?output "This should not be printed" printed.

?

I guess it is because in VS2010, it is deemed right to go the parent's scope when resolving copy constructor or assignment operator before it auto generate the constructor or assignment operator... And now this has been changed because this is too dangerous?

?

?

发表评论
用户名: 匿名