Boost logo

Boost :

From: Eric van Riet Paap (eric_at_[hidden])
Date: 2002-04-04 06:48:39


In C++ I have a class Node, Derived1 and Derived2, where I would like Node
to return a pointer to the actual derived instance.
I now have to cumbersome construct where the C++ return a pointer to a Node
and the Python code calls another function of
the Node class to get the correct derived class. Like so...

//Node.h
class Node
{
 static Node* Self (void) { return (Node*)gpCNode; } //use
Node.As...() or Global_.Instance() to convert it to the correct
class-type!!!
 Camera * AsCamera (void) { return (Camera *)this; }
//class-type convertion
 Mesh * AsMesh (void) { return (Mesh *)this; }
//class-type convertion
 Light * AsLight (void) { return (Light *)this; }
//class-type convertion
};

//Global_.py
def Instance(node):
         return eval('node.As'+node.SuperClassName()+'()')

What I would like to do is let the Node.Self() function return the corrent
type but I'm not actacly sure how to proceed.
I can think of two ways. The first one would be to return a PyObject and
building that in the Self function, which is something
I have never toyed with. The second one would be to use a virtual function
where all derived classes return the actual type,
but I'm not sure the actual type pointer will be return to python in this
case, which is something I will try to find out now.

Is my above text understandable and does anyone have tips or an example
perhaps?

kind regards,
Eric van Riet Paap
Utrecht, The Netherlands


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk