From: Nick Hounsome on 14 Feb 2010 08:31 On 12 Feb, 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 // derive from interface class A : public Interface > { > public: > void foo(); > > }; > > And then I could box this type like this: > Interface * a = box_cast<Interface*>(new A()); With the changes above Interface* a = new A; a->foo(); // calls A::foo This is just polymorphism not boxing. boxing is what .NET does to convert struct types (stack allocated in C+ +) to equivalent reference types (heap allocated in C++) and back. When generic containers came into .NET a lot of the need for boxing went away and there is no equivalent problem in C++ (in fact with C++ it's harder to manage containers of "reference types" (i.e. heap stuff) because you haven't got the garbage collection) The main surviving uses are for reflection type problems and generic Object.ToString stuff and you haven't given enough info for us to know why you think that you need it. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Juan Pedro Bolivar Puente on 14 Feb 2010 08:33 On 13/02/10 00: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()); > There is no general way to do it, but for your specific interface you can write something like: template <Boxed> class InterfaceBox : public Interface { std::auto_ptr<Boxed> m_boxed; public: InterfaceBox (Boxed* boxed) : m_boxed (boxed) {} void foo () { m_boxed->foo (); } }; template<Boxed> Interface* box_interface (Boxed* boxed) { return new InterfaceBox<Boxed> (boxed); } .... Interface* boxed_a = box_interface (new A); .... delete boxed_a; .... Of course, you can decide to have a different memory management policicy for the boxed type in case you still want to be able to use the A* intance in other parts of the program, like using a shared_ptr, or a raw pointer or whatever fits your needs. You could templatize that also... JP -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 15 Feb 2010 05:42 On 13 f�v, 23:23, Jeff Schwab <j...(a)schwabcenter.com> wrote: > "Type erasure" is the unavailability of some static type information at > run-time. No. Type erasure is "the process of turning a wide variety of types with a common interface into one type with that same interface." -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mark Zaytsev on 16 Feb 2010 11:01 > 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()); > > Maybe this is called something else in C++ It called "Inheritance": class A : public Interface { public: virtual void foo(); }; Interface* i = new A; // no need to cast -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Edward Diener on 16 Feb 2010 11:02 Jeff Schwab wrote: > Mathias Gaunard wrote: >> On 13 f�v, 23:23, Jeff Schwab <j...(a)schwabcenter.com> wrote: >> >>> "Type erasure" is the unavailability of some static type information at >>> run-time. >> >> No. Type erasure is "the process of turning a wide variety of types >> with a common interface into one type with that same interface." > > I don't know where you are getting that definition, but it is not the > one in common use. For example, see: > > http://java.sun.com/docs/books/tutorial/java/generics/erasure.html > > It's not just that multiple types have been consolidated behind a single > interface; it is that the original, static type information has actually > been lost. Java and C++ use the term "type erasure" differently, and Mathias is correct in explaining the C++ use of "type erasure". In Java, "type erasure" is used in regard to Java's generics, but C++'s templates are not analogous to the way that Java's generics work, so there is little reason why "type erasure" in C++ should mean the same thing you quoted in your Java link. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Determine type of member variable Next: global variables - an alternate approach |