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.

45 lines
937 B
C++

#include <iostream>
#include <memory>
#include "main.h"
template<typename T>
class Wrapper {
std::shared_ptr<T> impl;
public:
explicit Wrapper(std::shared_ptr<T> ptr) : impl(std::move(ptr)) {}
T* operator->() { return impl.get(); }
const T* operator->() const { return impl.get(); }
std::shared_ptr<T> get() const { return impl; }
operator std::shared_ptr<T>() const { return impl; }
operator const std::shared_ptr<T>&() const { return impl; }
operator std::shared_ptr<T>&() { return impl; }
void myHelper() const {
std::cout << "[Wrapper] Helper method called\n";
}
};
void draw_shape(const std::shared_ptr<IShape>& shape) {
shape->draw();
}
int main() {
auto circle = std::make_shared<Circle>(10.0);
Wrapper<IShape> wrapped(circle);
wrapped->draw();
std::cout << "Area: " << wrapped->area() << "\n";
wrapped.myHelper();
draw_shape(wrapped);
std::shared_ptr<IShape> copy = wrapped;
draw_shape(copy);
}