3

I have the following code (simplified):

#include <cstdio>

class parent
{
public:
  virtual void do_something() const
  {
    printf("hello I'm the parent class\n");
  }
};

class child : public parent 
{
public:
  virtual void do_something() const
  {
    printf("hello I'm the child class\n");
  }
};

void handle(parent p)
{
   p.do_something();
}

int main()
{
  child c;
  handle(c);
  return 0;
}

This prints hello I'm the parent class, even though I passed a argument of type child. How can I tell C++ to behave like Java does and invoke the method of the child, printing hello I'm the child class?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
msrd0
  • 7,816
  • 9
  • 47
  • 82

1 Answers1

9

Accept the argument by reference (or, probably, const reference):

void handle (parent & p)
//        note this ^
{
    p.do_something();
}

In your case, slicing happens: the parent part of the child gets extracted as a separate object of type parent and goes to the function.

If you want to put different subclasses into a single collection, a usual solution is to use smart pointers, such as std::unique_ptr or std::shared_ptr.

Community
  • 1
  • 1
lisyarus
  • 15,025
  • 3
  • 43
  • 68
  • The problem is that in the "big" code I am inserting the object into a `vector`, but there can be different subclasses in the same `vector`. Can I store those references in the vector? – msrd0 Apr 26 '16 at 17:29
  • 2
    @msrd0, no, you can't store references in the vector. You'd have to use `std::unique_ptr` as your vector element type, and store pointers to dynamically allocated objects. – SergeyA Apr 26 '16 at 17:30
  • 2
    In addition to @SergeyA's suggestion, you can also use [`std::reference_wrapper`](http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper). – R Sahu Apr 26 '16 at 17:32
  • 1
    @RSahu, this will unlikely to fly - it's hard to imagine all those automatic objecs will have a lifetime exceeding vector lifetime... – SergeyA Apr 26 '16 at 17:34
  • @SergeyA Actually, I've seen such use cases, but I agree that they are rare. – lisyarus Apr 26 '16 at 17:35