You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
780 B
C
51 lines
780 B
C
#include <iostream>
|
|
|
|
struct IShape {
|
|
virtual void draw() const = 0;
|
|
virtual ~IShape() = default;
|
|
};
|
|
|
|
class Circle : public IShape {
|
|
public:
|
|
void draw() const override {
|
|
std::cout << "Circle::draw()\n";
|
|
}
|
|
};
|
|
|
|
|
|
template<typename T>
|
|
class Wrapper {
|
|
T* impl;
|
|
|
|
public:
|
|
Wrapper(T* obj) : impl(obj) {}
|
|
|
|
T* operator->() { return impl; }
|
|
const T* operator->() const { return impl; }
|
|
|
|
operator T*() { return impl; }
|
|
operator const T*() const { return impl; }
|
|
|
|
operator T&() { return *impl; }
|
|
operator const T&() const { return *impl; }
|
|
};
|
|
|
|
void render_shape_by_ptr(IShape* shape) {
|
|
shape->draw();
|
|
}
|
|
|
|
void render_shape_by_ref(IShape& shape) {
|
|
shape.draw();
|
|
}
|
|
|
|
int main() {
|
|
Circle c;
|
|
Wrapper<IShape> w(&c);
|
|
|
|
w->draw();
|
|
|
|
render_shape_by_ptr(w);
|
|
|
|
render_shape_by_ref(w);
|
|
}
|