What is a possible workaround for object slicing in c++?

后端 未结 3 1984
情书的邮戳
情书的邮戳 2020-12-06 03:12

This is the code:

#include 
#include 
#include 
using namespace std;
class A {
public:
  virtual const string f()         


        
3条回答
  •  自闭症患者
    2020-12-06 03:38

    Well, in your code you could use a vector of B. Note that virtual functions will only be dispatched properly if called via a pointer or a reference. However, assuming you really want your vector to contain both A and B objects, you need to make it vector of A pointers, and create the A and B objects dynamically.

提交回复
热议问题