Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ vs. C++/CLI: Const qualification of virtual function parameters
    primarykey
    data
    text
    <p>[All of the following was tested using Visual Studio 2008 SP1]</p> <p>In C++, const qualification of parameter types does not affect the type of a function (8.3.5/3: "Any cv-qualifier modifying a parameter type is deleted")</p> <p>So, for example, in the following class hierarchy, <code>Derived::Foo</code> overrides <code>Base::Foo</code>:</p> <pre><code>struct Base { virtual void Foo(const int i) { } }; struct Derived : Base { virtual void Foo(int i) { } }; </code></pre> <p>Consider a similar hierarchy in C++/CLI:</p> <pre><code>ref class Base abstract { public: virtual void Foo(const int) = 0; }; ref class Derived : public Base { public: virtual void Foo(int i) override { } }; </code></pre> <p>If I then create an instance of <code>Derived</code>:</p> <pre><code>int main(array&lt;System::String ^&gt; ^args) { Derived^ d = gcnew Derived; } </code></pre> <p>it compiles without errors or warnings. When I run it, it throws the following exception and then terminates:</p> <blockquote> <p>An unhandled exception of type 'System.TypeLoadException' occurred in ClrVirtualTest.exe</p> <p>Additional information: Method 'Foo' in type 'Derived'...does not have an implementation.</p> </blockquote> <p>That exception seems to indicate that the const qualification of the parameter <em>does</em> affect the type of the function in C++/CLI (or, at least it affects overriding in some way). However, if I comment out the line containing the definition of <code>Derived::Foo</code>, the compiler reports the following error (on the line in <code>main</code> where the instance of <code>Derived</code> is instantiated):</p> <blockquote> <p>error C2259: 'Derived': cannot instantiate abstract class</p> </blockquote> <p>If I add the const qualifier to the parameter of <code>Derived::Foo</code> or remove the const qualifier from the parameter of <code>Base::Foo</code>, it compiles and runs with no errors.</p> <p>I would think that if the const qualification of the parameter affects the type of the function, I should get this error if the const qualification of the parameter in the derived class virtual function does not match the const qualification of the parameter in the base class virtual function.</p> <p>If I change the type of <code>Derived::Foo</code>'s parameter from an <code>int</code> to a <code>double</code>, I get the following warning (in addition to the aforementioned error, C2259):</p> <blockquote> <p>warning C4490: 'override': incorrect use of override specifier; 'Derived::Foo' does not match a base ref class method</p> </blockquote> <p>So, my question is, effectively, does the const qualification of function parameters affect the type of the function in C++/CLI? If so, why does this compile and why are there no errors or warnings? If not, why is an exception thrown?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload