Covariant virtual functions return type problem

空扰寡人 提交于 2019-12-05 22:20:34

You missed the other part they quoted: "If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria: (1) both are pointers to classes or references to classes"

They have to be references or pointers, not concrete classes. That requirements could live quite happily inside the "convariant with" stipulation in your MS quote. The second part speaks about "the class in the return type" - note the care with which they've avoided "the class returned" precisely because it's the class component of the pointer- or reference-to-class return type.

Think about what covariance is for: you're getting back an object and some existing code written for the base class may be wishing to handle that but through virtual dispatch end up getting one of your objects. If they could be different in size how could the objects be stored? This mess is sidestepped by the indirection... you can decide where they are (some buffer, shared mem, heap) and the covariant return type is the pointer or reference thereto.

If you are changing string conversions like std::string to std::wstring or any string conversions of virtual functions, you need to change super class of that virtual function where actually that virtual function is declared. You need to change every where where you are using function in which you are changing string conversions.

In my case that is happened hope it will help some one.. .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!