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

Tip: Looking for answers? Try searching our database.

Managed Wrapper with abstract classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WithPit - 27 May 2005 08:27 GMT
I am trying to create an managed wrapper but have some problems with it by
using abstract classes.

In my unmanaged library code i had the following three classes with the
following hierarchy

Referenced (class)
Object (abstract class, inheriting from referenced)
Node (class, inheriting from object)

I want to make an managed wrapper for it with the same type of classes. So i
created an managed referenced class, and because this is the base class i
give it the pointer to the unmanaged object referenced.

The object class will be an managed abstract class with no constructor,
because abstract class should not have one. One of the methods of the
unmanaged class is cloneObject which should be implemented by the classes
that are inheriting of the abstract class Node. CloneObject gives as result
an value of the type Object.

So now the problem, i create an managed class Node which implements the
cloneObject method. But how to define the body of this method. Because with
not abstract classes i have created an extra constructor which get the
unmanagedobject en wrapped it. But with an abstract return type this is not
possible.

Hope this gives you more details about my question, and i hope somebody had
an solution for it.

Thanx

WithPit
ismailp - 27 May 2005 15:28 GMT
Excuse me, but I couldn't understand the problem at all. It would be
better if you can provide a sample.

The abstract class can have a constructor, there is no restriction
about it. Interfaces and abstract classes are different, although they
have much in common. Abstract classes can contain data members, can
have virtual methods but those methods are required to be virtual.
However, abstract classes may contain one or more pure virtual methods,
which prevents you to instantiate the class. Therefore, instantiating
Node should be considered, rather than Object.

so,
__gc class Referenced
{

};

__abstract __gc class Obj
{
public:
    Obj()
    {
    }
    virtual Obj* cloneObject() = 0; // pure virtual
};

__gc class Node : public Obj
{
public:
    virtual Obj* cloneObject()
    {
        Node* pnode = new Node;
        // implementation (perhaps, copying)
        return pnode;
    }
};

should work fine
WithPit - 27 May 2005 22:08 GMT
The problem is that the managed code is calling the unmanaged code. So for
the referenced class this means that it has an pointer to the unmanaged object

__gc class Referenced
{
        unmanagedReferenced* _unmanagedObject;
};

with probaly a constructor, but that doesn't mater. The abstract class
object inheritance from Referenced. Like you say

__abstract __gc class Obj, public Referenced
{
public:
    Obj()
    {
    }
    virtual Obj* cloneObject() = 0; // pure virtual
};

And the Node class implements the method cloneObject().

__gc class Node : public Obj
{
public:
    virtual Obj* cloneObject()
    {
        return new Obj(static_cast<unmanagedNode*>_unmanagedobject->cloneObject());
    }
};

For this a constructor is needed with has an unmanaged object as argument.
That is not the problem. The point is that the unmanaged object is wrapped
inside the managed object but from the outside you don't know the type
anymore. You create a new Node object inside your example but maybe there are
methods when you don't know the exact return type. Could this not be a
problem for the managed wrapper? Or is this implementation the best? Or see i
things the wrong way.

Bye

WithPit

> Excuse me, but I couldn't understand the problem at all. It would be
> better if you can provide a sample.
[quoted text clipped - 34 lines]
>
> should work fine
WithPit - 27 May 2005 22:19 GMT
I forgot something, because with this implementation the Obj class cannot be
abstract because of the fact that it is now instantiated. I wanted to created
an managed wrapper with the same class structure so that abstract unmanaged
classes are also abstract in the managed wrapper and no objects could be
created of it.

You don't know wat type the unmanaged method returns, you only know the
abstract base type. So my question is, is there an implementation possible
with the use of abstract classes like Obj in this example?

Bye

WithPit

> The problem is that the managed code is calling the unmanaged code. So for
> the referenced class this means that it has an pointer to the unmanaged object
[quoted text clipped - 77 lines]
> >
> > should work fine

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.