MC++ FAQ
Tuesday 04 | 15 | 2003
Managed Types in Virtual Methods of Unmanaged Types
Unmanaged (__nogc) types in MC++ don't support __gc classes as arguments of managed member methods that are also virtual. Here's a small sample that illustrates the problem:
#using <mscorlib.dll>
using namespace System;

__nogc class Base
{
public:
   virtual void Print(String __gc* s)
   {
      Console::WriteLine(s);
   }
};

__nogc class Derived : public Base
{
public:
   virtual void Print(String __gc* s)
   {
      __super::Print(s);
      Console::WriteLine(String::Concat(s, S" - Derived"));
   }
};

int main()
{
   Base* b = new Derived;
   b.Print(S"Message");
}

Compiling this will cause the following error messages:

E:\temp>cl /clr t.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 13.10.3077 for .NET Framework
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

t.cpp
t.cpp(10) : error C3383: 'Base::Print' : in an unmanaged class, a virtual member
 function cannot have a managed type in the signature
t.cpp(19) : error C3383: 'Derived::Print' : in an unmanaged class, a virtual mem
ber function cannot have a managed type in the signature
t.cpp(28) : error C2228: left of '.Print' must have class/struct/union type
        type is 'Base *'
        did you intend to use '->' instead?

One possible reason for this is that in current incarnations of MC++, calls to virtual managed methods in __nogc classes are always dispatched through a managed-unmanaged-managed code transitions (see also this)

Posted by tomasr at April 15, 2003 01:31 AM
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?

Copyright(C) 2002, Tomas Restrepo, All rights reserved. Powered by Movable Type 2.63