add iterable
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user