c++ - 我们可以超越对象切片吗?

标签 c++ inheritance object-slicing

免责声明:仅当您想了解未完成的情况时才继续阅读。显然这是错误的,当你看到它时你会因此患上眼癌(我是免疫的,因为我是一个菜鸟 :)


所以我考虑了一段时间 C++ 中的运行时多态性,然后想到了以下内容。考虑这个

#include <iostream>

struct Animal
{
    virtual ~Animal(){}
    void make_noise() const {return do_make_noise();}
    protected:
    Animal( ){}
    virtual void do_make_noise()const{std::cout << "Meh\n";}
};
struct Cat:public Animal
{
    void do_make_noise()const{ std::cout<< "meow\n";}
};
struct Dog:public Animal
{
    void do_make_noise()const{ std::cout<< "woof\n";}
};

int main()
{
    Cat cat;
    Dog dog;
    Animal a((const Animal&)cat); //every tutorial teaches us that this is bad!!
    a.make_noise();
    a = (const Animal&)dog;
    a.make_noise();
    return 0;
}

如你所料,输出是

Meh
Meh

好的,对象切片很糟糕:/
但是现在让我稍微更改一下基类:

struct Animal
{
    virtual ~Animal(){}
    void make_noise() const {return ptr_->do_make_noise();}
    protected:
    Animal( ):ptr_(this){}
    Animal* ptr_;
    virtual void do_make_noise()const{std::cout << "Meh\n";}
};

那么结果就是

meow
woof

哈哈。螺旋对象切片 ...
你怎么看呢?在我看来,能够按值复制并仍然获得多态行为有很多优点。人们不使用它有什么原因吗?或者我只是在这里重新发明轮子?

最佳答案

您编写的代码没有表现出未定义的行为,并且确实“阻止对象切片”;相反,它非常脆弱且无法维护。

每个 Animal 现在都拥有一个非托管指针,指向它充当的某个 ur-Animal(有时是它自己)。您的代码违反了合理复制和移动构造的含义。

实际上,它避免了崩溃和未定义的行为,但只是偶然发生的。看似无害的小调整和您的程序中断。

写一个返回动物的函数?您的代码会中断,具体取决于省略。从本地 Animal 复制到传入的引用,然后返回? splinter 的。把它们放在标准 vector 中?彻底的灾难。

创建不受切片影响的值语义多态类型看起来有点像这样,但是您将虚拟层次结构从拥有值类型的 pImpl 中分离出来,实例拥有 pImpl 指向的内容。

struct IAnimal{
  virtual void speak() const=0;
  virtual std::unique_ptr<IAnimal> clone() const = 0;
  virtual ~IAnimal(){}
};
struct Animal {
  std::unique_ptr<IAnimal> pImpl;
  void speak() const { if (pImpl) pImpl->speak(); }
  explicit operator bool()const{return (bool)pImpl;}
  Animal(Animal&&)=default;
  Animal& operator=(Animal&&)=default;
  Animal(Animal const&o):
    Animal(o.pImpl?Animal(o.pImpl->clone()):Animal())
  {}
  Animal()=default;
  Animal& operator=(Animal const& o){
    Animal tmp(o);
    swap(pImpl, tmp.pImpl);
    return *this;
  }
  Animal(std::unique_ptr<IAnimal> x):pImpl(std::move(x)){}
};

现在 Animal 不能被切片并且可以是多态的。

struct Dog:Animal{
  struct IDog:IAnimal{
    std::unique_ptr<IAnimal> clone()const final override{ return std::make_unique<IDog>(*this); }
    void speak()const final override { std:cout <<"woof\n"; }
  };
  Dog():Animal(std::make_unique<IDog>()){}
};  
struct Cat:Animal{
  struct ICat:IAnimal{
    std::unique_ptr<IAnimal> clone()const final override{ return std::make_unique<ICat>(*this); }
    void speak()const final override { std:cout <<"meow\n"; }
  };
  Cat():Animal(std::make_unique<ICat>()){}
};

除了 tpyos 我们得到:

Cat cat;
Dog dog;
Animal a((const Animal&)cat); //every tutorial teaches us that this is bad!!
a.speak();
a = (const Animal&)dog;
a.speak();

工作。

而且我的复制/移动 ctors 是明智的,我在 vector 中工作,而且我没有到处都是悬空指针。

关于c++ - 我们可以超越对象切片吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45259360/

相关文章:

c++ - 缺少复制构造函数与对象切片有何关系?

c++ - 具有迭代器和继承的 STL 容器

复杂浮点类型的 C++20 概念

c++ - 错误 PRJ0002 : Error result -1073741515 returned from 'C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe'

c++ - 添加自定义析构函数时, move 构造函数在派生类中消失

oop - 强制派生类调用 MATLAB 中的基函数?

c++ - 将派生对象分配给函数内的基类指针

非专用模板类型对象的 C++ 列表?

c++ - 将指针转换为shared_ptr

c# - Reflection.Emit 创建通用继承方法