
I'm having a problem with Boost.Tuple virtual functions. Consider the code below, I would have expected the output to be "B::foo()" but instead get "A::foo()". Am I missing something here? #include <boost/tuple/tuple.hpp> #include <iostream> class A { public: virtual void foo() { std::cout << "A::foo()" << std::endl; } }; class B :public A { public: void foo() { std::cout << "B::foo()" << std::endl; } }; boost::tuple< A > make( const A& a ) { return boost::make_tuple( a ); } int main( int argc, char* argv[] ) { B b; make( b ).get< 0 >().foo(); return 0; }

On 12 Jan 2012, at 13:30, Mark Snelling wrote:
I'm having a problem with Boost.Tuple virtual functions. Consider the code below, I would have expected the output to be "B::foo()" but instead get "A::foo()". Am I missing something here?
Yes, if you copy a A&, you end up with an A. This is known as 'slicing', and can be a pain. It has nothing to do with boost, or tuple. Chris
#include <boost/tuple/tuple.hpp> #include <iostream>
class A { public: virtual void foo() { std::cout << "A::foo()" << std::endl; } };
class B :public A { public: void foo() { std::cout << "B::foo()" << std::endl; } };
boost::tuple< A > make( const A& a ) { return boost::make_tuple( a ); }
int main( int argc, char* argv[] ) { B b; make( b ).get< 0 >().foo();
return 0; }
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Thu, Jan 12, 2012 at 2:35 PM, Christopher Jefferson <chris@bubblescope.net> wrote:
On 12 Jan 2012, at 13:30, Mark Snelling wrote:
I'm having a problem with Boost.Tuple virtual functions. Consider the code below, I would have expected the output to be "B::foo()" but instead get "A::foo()". Am I missing something here?
Yes, if you copy a A&, you end up with an A. This is known as 'slicing', and can be a pain. It has nothing to do with boost, or tuple.
Indeed. Derive A from boost::noncopyable to catch this mistake. Olaf
participants (3)
-
Christopher Jefferson
-
Mark Snelling
-
Olaf van der Spek