Polymorphism and reference class member - wrong virtual method called

Hakim

I was trying to understand how to apply polymorphism in C++ by creating a base class Document from which the two classes Book and Newspaper are derived. Note how the virtual method get_content() is overridden inside the derived classes.

class Document {
public:
  virtual std::string get_content() const { return ""; }
};

class Book : public Document {
public:
  std::string get_content() const override { return "Book"; }
};

class Newspaper: public Document {
public:
  std::string get_content() const override { return "Newspaper"; }
};

The Document class is aggregated with the Printer class (by reference), and the get_content() method of the appropriate derived class is called according to the type of document.

class Printer {
public:
  const Document& m_doc;

  Printer(const Document& doc): m_doc(doc) {
    std::cout << "Printing... " << m_doc.get_content() << std::endl;
  }

  std::string get_content() const { return m_doc.get_content(); }
};

Until now everything works well, but once I aggregate the Printer class with another class Binding, the get_content() doesn't seem to call the method of the right derived class anymore.

class Binding {
public:
  Printer m_printer;

  Binding(const Printer& printer): m_printer(printer) {
    std::cout << "Binding... " << m_printer.get_content() << std::endl;
  }
};

I don't understand why the following piece of code...:

int main() {
  Printer p1(Book{});
  Printer p2(Newspaper{});
  Binding b1(p1);
  Binding b2(p2);
}

...shows Binding... Newspaper at the third line while the Printing is clearly getting a Book instance as an input:

Printing... Book
Printing... Newspaper
Binding... Newspaper
Binding... Newspaper

Having m_printer declared as a reference inside Binding class doesn't fix this problem either.

Yakk - Adam Nevraumont
Printer p1(Book{});
Printer p2(Newspaper{});

Here, you are using temporary objects. These objects are destroyed at the end of the statement they are created in.

So, you have dangling references. Calling methods on a dangling reference is undefined behaviour.

In C++, you are in charge of object lifetime.

(Reference lifetime extension does not apply here)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why a member method of class is called before the Constructor

From Dev

Polymorphism and Virtual method

From Dev

Issue with Using Polymorphism While Only Child Class has a Virtual Method

From Dev

Issue with Using Polymorphism While Only Child Class has a Virtual Method

From Dev

Android: passing member to method of another class as reference?

From Dev

calling a virtual function through a reference: derived class' override gets called

From Dev

Polymorphism and inheritance using class reference?

From Dev

Implemented virtual method call fails when called through base class

From Dev

Reference initialization as member of class

From Dev

Polymorphism:what is the real type of the called method?

From Dev

How to check if member method is called

From Dev

dynamic polymorphism reference pointing to base class

From Dev

dynamic polymorphism reference pointing to base class

From Dev

Member in child class a reference to parent member?

From Dev

Member in child class a reference to parent member?

From Dev

Reference to non static member function must be called

From Dev

Class reference member list initialization

From Dev

Ruby class called as a method

From Dev

Callback not called if it is a class method

From Dev

Class Member Not a Reference but Constructor with Argument Passed by Reference

From Dev

Virtual method pattern that forces base method to be called

From Dev

Ruby: call method of current class without polymorphism

From Dev

How to set weak reference to a class member e.g. callback method?

From Dev

How to set weak reference to a class member e.g. callback method?

From Dev

C#: Inherited class with methods taking Child type as a parameter: wrong method being called

From Dev

Inherited class with methods taking Child type as a parameter: wrong method being called

From Dev

Django Rest Framework: `get_serializer_class` called several times, with wrong value of request method

From Dev

Passing class member to base class constructor (by reference)

From Dev

Reference to class method

Related Related

  1. 1

    Why a member method of class is called before the Constructor

  2. 2

    Polymorphism and Virtual method

  3. 3

    Issue with Using Polymorphism While Only Child Class has a Virtual Method

  4. 4

    Issue with Using Polymorphism While Only Child Class has a Virtual Method

  5. 5

    Android: passing member to method of another class as reference?

  6. 6

    calling a virtual function through a reference: derived class' override gets called

  7. 7

    Polymorphism and inheritance using class reference?

  8. 8

    Implemented virtual method call fails when called through base class

  9. 9

    Reference initialization as member of class

  10. 10

    Polymorphism:what is the real type of the called method?

  11. 11

    How to check if member method is called

  12. 12

    dynamic polymorphism reference pointing to base class

  13. 13

    dynamic polymorphism reference pointing to base class

  14. 14

    Member in child class a reference to parent member?

  15. 15

    Member in child class a reference to parent member?

  16. 16

    Reference to non static member function must be called

  17. 17

    Class reference member list initialization

  18. 18

    Ruby class called as a method

  19. 19

    Callback not called if it is a class method

  20. 20

    Class Member Not a Reference but Constructor with Argument Passed by Reference

  21. 21

    Virtual method pattern that forces base method to be called

  22. 22

    Ruby: call method of current class without polymorphism

  23. 23

    How to set weak reference to a class member e.g. callback method?

  24. 24

    How to set weak reference to a class member e.g. callback method?

  25. 25

    C#: Inherited class with methods taking Child type as a parameter: wrong method being called

  26. 26

    Inherited class with methods taking Child type as a parameter: wrong method being called

  27. 27

    Django Rest Framework: `get_serializer_class` called several times, with wrong value of request method

  28. 28

    Passing class member to base class constructor (by reference)

  29. 29

    Reference to class method

HotTag

Archive