add iterable
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
#include <iostream>
|
||||
#include "main.h"
|
||||
|
||||
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 circle {10};
|
||||
Wrapper<IShape> wrapped(&circle);
|
||||
|
||||
std::cout << wrapped->area() << "\n";
|
||||
|
||||
render_shape_by_ptr(wrapped);
|
||||
|
||||
render_shape_by_ref(wrapped);
|
||||
}
|
||||
Reference in New Issue
Block a user