Hi all, the code below doesn't do what I would expect and I'm having some problems understanding why. I was expecting to see "Bar method" as output however it gives "Foo method", also if I use a pointer to Foo in Testing class it works as expected.
Thanks!Code:#include <iostream> class Foo { public: Foo() {} virtual ~Foo() {} virtual void method() {std::cout << "Foo method\n";} }; class Bar: public Foo { public: Bar() {} ~Bar() {} virtual void method() { std::cout << "Bar method\n";} }; class Testing { public: Testing() {} ~Testing() {} void setFoo(Foo &f) {f_ = f;} void start() {f_.method();} private: Foo f_; }; int main() { Foo f0; Bar b0; Testing t0; t0.setFoo(b0); t0.start(); return 0; }



LinkBack URL
About LinkBacks



