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?
讨论了从VS10到VS12,拷贝构造函数和赋值运算符在类层次结构中行为的变化,导致在特定情况下输出异常现象。解释了此更改的原因及影响。
1307

被折叠的 条评论
为什么被折叠?



