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++ / November 2005

Tip: Looking for answers? Try searching our database.

wrapping an abstract base class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lee Crabtree - 04 Nov 2005 20:30 GMT
More fun wrapping unmanaged code...

I have a class heirarchy that I need to expose to C#, so I need to wrap all
the classes.  That's not a big deal, except for the base class, which is
abstract.  Since you can't instantiate an abstract class, I can't use a
pointer to the unmanaged type to handle the function calls.

Is there a good way to do this?

Lee
Brian Muth - 04 Nov 2005 21:10 GMT
Since you can't instantiate an abstract class, it makes no sense to wrap it
(expose it to C#) since you can't use it anyway. So why do you feel
compelled to wrap it?

Brian
Lee Crabtree - 04 Nov 2005 21:23 GMT
That's an excellent point that hit me two minutes after I posted my message.
I guess I've just done so much wrapping in the last week or so that I've got
wrap-happy.

Lee

> Since you can't instantiate an abstract class, it makes no sense to wrap
> it (expose it to C#) since you can't use it anyway. So why do you feel
> compelled to wrap it?
>
> Brian
Lee Crabtree - 11 Nov 2005 18:52 GMT
I've found at least one reason why wrapping an abstract base class makes
sense.

Say the base class for several different other classes contains a lot of
functions that are inherited without being reimplemented.  Without wrapping
the base class, you would have to wrap those functions in every single
inherited class.  That seems like a lot of unnecessary work.

In C++, an abstract class can still have function definitions that actually
perform operations.  Redefining those functions in every single derived
class is definitely unneeded work.

Any ideas?

Lee

> Since you can't instantiate an abstract class, it makes no sense to wrap
> it (expose it to C#) since you can't use it anyway. So why do you feel
> compelled to wrap it?
>
> Brian
Tamas Demjen - 11 Nov 2005 22:02 GMT
> Say the base class for several different other classes contains a lot of
> functions that are inherited without being reimplemented.  Without wrapping
> the base class, you would have to wrap those functions in every single
> inherited class.  That seems like a lot of unnecessary work.

In that case consider my example. I introduced a method called
GetUnmanaged(), which is supposed to return the underlying native
implementation. In the abstract base, you don't know that pointer yet,
but in the derived classes you can just implement GetUnmanaged, which is
a trivial implementation. Can you live with that? ExecuteTwice did not
have to be duplicated in the derived classes.

Note that I used the new C++/CLI syntax. If you're using VC++ 2003,
you'll have to edit my code, but the concept is the same. This should
get you started.

Tom

// Wrap1.cpp : main project file.
// Output of the application is:
//   UnmanagedDerived::Execute
//   UnmanagedDerived::Execute

#include "stdafx.h"
#include <iostream>

using namespace System;

// native:

class UnmanagedBase
{
public:
   virtual ~UnmanagedBase() { }
   virtual void Execute() = 0;
   void ExecuteTwice() { Execute(); Execute(); }
};

class UnmanagedDerived : public UnmanagedBase
{
public:
   virtual void Execute() { std::cout << "UnmanagedDerived::Execute\n";
   }
};

// managed:

ref class ManagedBased abstract
{
public:
   virtual void Execute() { GetUnmanaged()->Execute(); }
   void ExecuteTwice() { GetUnmanaged()->ExecuteTwice(); }
protected:
   virtual UnmanagedBase* GetUnmanaged() = 0;
};

ref class ManagedDerived : public ManagedBased
{
public:
   ManagedDerived() : ptr(new UnmanagedDerived) { }
   ~ManagedDerived() { delete ptr; }
protected:
   virtual UnmanagedBase* GetUnmanaged() override { return ptr; }
private:
   UnmanagedDerived* ptr;
};

int main(array<System::String ^> ^args)
{
   ManagedDerived md;
   md.ExecuteTwice();
   return 0;
}
Olaf Baeyens - 09 Nov 2005 14:59 GMT
> I have a class heirarchy that I need to expose to C#, so I need to wrap all
> the classes.  That's not a big deal, except for the base class, which is
> abstract.  Since you can't instantiate an abstract class, I can't use a
> pointer to the unmanaged type to handle the function calls.

Abstract classes are half-built classes.
You must inherit from it and define the missing functionality.
Your compiler will tell you what methods needs to be created that are
incomplete in the abstract class.

Rate this thread:







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.