Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

Inherit C++ interface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aleko - 18 Oct 2007 08:15 GMT
Hi,

I am trying to wrap some std C++ code so it can be used from C#. The
problem I'm having is that I can't seem to derive a ref class from a C+
+ interface. Compilation fails with C2811: "...can only inherit from a
ref class or interface class". AFAIK an abstract struct constitutes a
valid interface.
Here's an example:

// std C++
namespace blah
{
struct IThing
{
 virtual ~IThing() {}
 virtual void doIt() = 0;
};

class Thing : public IThing
{
public:
 Thing() {}
 virtual ~Thing() {}
 void doIt() {}
};
} // namespace

// C++/CLI
namespace Blah
{
ref class Thing : public blah::IThing // C2811
{

};
}

Replacing the structs with __interfaces didn't help. Any ideas?

Thanks,

Aleko
David Lowndes - 18 Oct 2007 09:25 GMT
>I am trying to wrap some std C++ code so it can be used from C#. The
>problem I'm having is that I can't seem to derive a ref class from a C+
>+ interface. Compilation fails with C2811: "...can only inherit from a
>ref class or interface class". AFAIK an abstract struct constitutes a
>valid interface.

The error message possibly only means one defined using the interface
name - see "interface class" in MSDN.

Dave
Ben Voigt [C++ MVP] - 18 Oct 2007 15:24 GMT
> Hi,
>
[quoted text clipped - 3 lines]
> ref class or interface class". AFAIK an abstract struct constitutes a
> valid interface.

An interface it may be, but a native C++ interface.  .NET classes can only
derive from .NET classes and .NET interfaces, hence the error message
referring to "ref class" and "interface class" (although "ref struct" and
"interface struct" would be allowed as well).  C++/CLI code can work with
both native and managed data, but a managed class cannot derive from a
native anything.  You'll have to *wrap* instead, meaning containment, which
is supported.

The basic problem is that .NET classes can be moved around by the garbage
collector.  If that .NET class had a native base class, its "this" pointer
could change at any time.  Native code isn't equipped to deal with this --  
it doesn't emit the metadata the garbage collector needs to invisibly adjust
all the pointers and references to "track" the new memory address of the
object.

> Here's an example:
>
[quoted text clipped - 30 lines]
>
> Aleko

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.