gpt4 book ai didi

c++ - 如何复制多态对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:02:25 26 4
gpt4 key购买 nike

函数“foo”来自外部库(在我的例子中是 DEAlII )。它以类类型作为模板参数及其对象。然后它创建该对象的一个​​拷贝并对该对象执行一些操作。

问题是输入类是多态的(模板)。然后我总是传递基类对象但指向不同的派生类,但是函数“foo”中复制的对象将是基类。对这个复制对象的成员函数的任何调用都将调用基类成员函数(但我需要它们调用相应的派生类成员函数)。

此外,派生类的类型是在运行时根据一些输入参数决定的。我不能更改/移动到不同的库,但我应该能够修改库中的函数“foo”(最好不要,但可以作为最后的手段)。

#include <iostream> 
#include <memory>
using namespace std;
class Shape {
protected:
int width, height;

public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}

void set(int a){
width =a ;
}

virtual int area() {
cout << "Parent class area :" <<endl;
return 0;
}
virtual ~Shape()=default;
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }

int area () override{
cout << "Rectangle class area :" <<width*height <<endl;
return (width * height);
}
};

class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
Triangle(const Triangle &triangle){
width = triangle.width;
height = triangle.height;
}
};

template <class temp>
void foo (temp &shape){
shape.area();
temp shape_sample = shape;
shape_sample.area();
}
// Main function for the program
int main() {
unique_ptr<Shape> shape;
Rectangle rec(10,7);

shape =make_unique<Rectangle> (rec);

foo (*shape.get());
shape->area();
return 0;
}

最佳答案

如果无法更改 foo 的实现,那么我会在此处看到两个选项:

选项 1:如果您知道它是什么,只需向下转换为适当的类型:

foo(static_cast<Rectangle &>(*shape.get()));

选项 2(矫枉过正):使用 Bridge pattern 隐藏多态性:

class Shape {
protected:
class ShapeImpl {
public:
int width, height;
virtual ~ShapeImpl() = default;

// "virtual copy constructor" which you could've used without a Bridge
// if you could change "foo"
virtual ShapeImpl *clone() { return new ShapeImpl(*this); }

virtual int area() {
cout << "Parent class area :" <<endl;
return 0;
}
} *impl; // can't use unique_ptr because we want copy

Shape(ShapeImpl *impl)
: impl(impl) { }

public:
Shape(const Shape &other)
: impl(other.impl ? other.impl->clone() : nullptr) {
}
Shape(Shape &&other)
: impl(nullptr) {
std::swap(impl, other.impl);
}
virtual ~Shape() {
delete impl;
}
// copy-and-swap idiom with one assignment operator to "rule-of-five them all"
Shape &operator=(Shape other) {
std::swap(impl, other.impl);
return *this;
}
int area() {
return impl->area();
}
};

class Rectangle : public Shape {
protected:
class RectangleImpl : public ShapeImpl {
public:
ShapeImpl *clone() override { return new RectangleImpl(*this); }
int area() override {
cout << "Rectangle class area :" <<width*height <<endl;
return (width * height);
}
};
public:
Rectangle(int width = 0, int height = 0)
: Shape(new RectangleImpl())
{
impl->width = width;
impl->height = height;
}
};

关于c++ - 如何复制多态对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57145160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com