Managed Wrapper with abstract classes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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
 
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
 
Back
Top