From: pfultz2 on
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());

perhaps,
class B : public A, public virtual Interface
{
};

Maybe this is called something else in C++, because everytime i google
c++ boxing it refers me to .Net boxing, which is a similiar since a
struct in c# that has an interface isnt polymorphic, so it boxes the
type, essentially creating a new type that inherits that interface. Is
there a way to do something similiar in standard c++?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 12 f�v, 22:43, pfultz2 <pful...(a)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());
>
> perhaps,
> class B : public A, public virtual Interface
> {
>
> };
>
> Maybe this is called something else in C++, because everytime i google
> c++ boxing it refers me to .Net boxing, which is a similiar since a
> struct in c# that has an interface isnt polymorphic, so it boxes the
> type, essentially creating a new type that inherits that interface. Is
> there a way to do something similiar in standard c++?

I think what you're looking for is called type erasure.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Martin B. on
On 12.02.2010 23:43, pfultz2 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());
>
> perhaps,
> class B : public A, public virtual Interface
> {
> };
>
> (...)Is
> there a way to do something similiar in standard c++?
>

I just stumbled over this snippet by Thiago A.:
http://www.thradams.com/codeblog/interface_cast.htm

Somehow seems to be what you had in mind.

Note that your ecact exampe can't work though, since to properly delete
you new'ed A you have to keep a ref/ptr to the actual A object.

br,
Martin

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jeff Schwab on
Mathias Gaunard wrote:
> On 12 f�v, 22:43, pfultz2 <pful...(a)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! ]

From: peter koch larsen on
On 12 Feb., 23:43, pfultz2 <pful...(a)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());
>
> perhaps,
> class B : public A, public virtual Interface
> {
>
> };
>
> Maybe this is called something else in C++, because everytime i google
> c++ boxing it refers me to .Net boxing, which is a similiar since a
> struct in c# that has an interface isnt polymorphic, so it boxes the
> type, essentially creating a new type that inherits that interface. Is
> there a way to do something similiar in standard c++?

Why do you need this? For what you have told it looks like you don't
need anything at all.

Interface* i = new A;

is perfectly valid C++ code.

/Peter


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]