Default copy constructor does not call correct base(轉) constructor
Section 12.8.8 of the C++ standard specifies that a default generated copy constructor should call the copy constructors of its base members.
However, as can be seen by running the following code
struct B {
B() {}
template B(const T&) { cout << "In B(const T&)\n"; }
B(const B&) { cout << "In copy constructor\n"; }
};
struct D : B {
D() {}
};
int main() {
D d1;
D d2(d1);
}
the default copy constructor of D calls the template constructor of B instead of B's copy constructor. It appears that root of the problem is that the compiler is generating this:
D::D(const D &rhs) : B(rhs) {}
which will perform. overload resolution and pick the template constructor. Instead, the compiler needs to generate something like this:
D::D(const D &rhs) : B(static_cast(rhs)) {}
Writing the above code is also the explicit workaround for this bug.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24104518/viewspace-721685/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PatchObject constructor:Input file does not existObjectStruct
- Effective C++是copy constructor 還是 copy assignment(“=”)的判斷C++Struct
- Prototype/ConstructorStruct
- JavaScript constructorJavaScriptStruct
- vuex報錯 vuex__WEBPACK_IMPORTED_MODULE_1__.default.store is not a constructorVueWebImportStruct
- get_constructorStruct
- constructor和superStruct
- TypeError: SizeOnlySource is not a constructorErrorStruct
- Lombok 之 ConstructorLombokStruct
- Angular 2 constructor & ngOnInitAngularStructGo
- React 中constructor 作用ReactStruct
- spring No default constructor found; nested exception is java.lang.NoSuchMethodException: com.slj.moSpringStructExceptionJava
- Javascript - prototype、__proto__、constructorJavaScriptStruct
- C++物件模型:constructorC++物件模型Struct
- js報錯:TypeError: Date is not a constructorJSErrorStruct
- javascript constructor簡單介紹JavaScriptStruct
- 7.107 JSON Type ConstructorJSONStruct
- constructor 未指向建構函式Struct函式
- 什麼是 constructor signature in interfaceStruct
- [譯]React ES6 class constructor super()ReactStruct
- 什麼是 TypeScript 裡的 Constructor signatureTypeScriptStruct
- C++基礎建構函式(constructor)C++函式Struct
- 深入分析js中的constructor 和prototypeJSStruct
- 原型鏈、_ptoto_、prototype、constructor的學習原型Struct
- javascript原型繼承constructor需要注意的地方JavaScript原型繼承Struct
- Spring - constructor-arg和property的使用示例SpringStruct
- [CareerCup] 14.1 Private Constructor 私有構建函式Struct函式
- 幫你徹底搞懂JS中的prototype、__proto__與constructor(圖解)(轉)JSStruct圖解
- Java之建立物件>4.Enforce noninstantiability with a private constructorJava物件Struct
- JavaScript學習筆記之constructor,prototype,__proto__解惑JavaScript筆記Struct
- JS 系列二:深入 constructor、prototype、__proto__、[[Prototype]] 及 原型鏈JSStruct原型
- 理解 es6 class 中 constructor 方法 和 super 的作用Struct
- 簡易版的 Spring 之如何實現 Constructor 注入SpringStruct
- 你真的會用ABAP, Java和JavaScript裡的constructor麼?JavaScriptStruct
- VUE前端打包報錯:TypeError: Class extends value undefined is not a constructor or nullVue前端ErrorUndefinedStructNull
- 用自己的方式(圖)理解constructor、prototype、__proto__和原型鏈Struct原型
- 一張圖理解prototype、proto和constructor的三角關係Struct
- React元件:為什麼呼叫順序是constructor -> willMount -> render -> DidMountReact元件Struct