Re: C++ boxing
Mathias Gaunard wrote:
On 12 f?v, 22:43, pfultz2 <pful...@yahoo.com> wrote:
Is there a way to box a type in c++ like this:
class Interface
{
public:
virtual void foo() = 0;
};
class A
{
public:
void foo();
};
And then I could box this type like this:
Interface * a = box_cast<Interface*>(new A());
I am not aware of any automated way to do what you are asking. The
show-stopper issue is that there is no way for a C++ template
metaprogram to get a list of the virtual functions in a type, to
override them.
The need for this exact language feature should be pretty rare. This
seems like a special case of the Adapter pattern, only useful in the
case where some class already happens to implement exactly the right
function signatures, only non-virtually. I suspect that you will find
other C++ language features eliminate the need for this one.
perhaps,
class B : public A, public virtual Interface
{
};
That is the right track.
I think what you're looking for is called type erasure.
"Type erasure" is the unavailability of some static type information at
run-time. The OP wants a form of code generation similar to templates,
where a new class can be created on demand.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]