C++/CLI: Is overloading on return type only possible?

本秂侑毒 提交于 2019-12-08 20:57:33

This type of overloading is allowed in C# not because of different return type, but because of explicit implementation of interface - ICloneable.Clone.

About C++/CLI look here: http://msdn.microsoft.com/en-us/library/ms235235%28VS.80%29.aspx

I finally found a way:

public ref class X : public ICloneable
{
    virtual System::Object^ Clone2() sealed = ICloneable::Clone;
public:
    X(X const&); // Traditional C++ copy constructor
    X^ Clone();
};

System::Object^ X::Clone2() { return this->Clone(); }
X^ X::Clone() { return gcnew X(*this); }

In C++ you can just leave out the second line, C++ allows covariance in overrides. Since X Clone() is compatible with the contract for object ICloneable::Clone(), it can put it directly into the v-table without needing a forwarding function.

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