From 765bfc3502e7de25cfdacc60b3aad4df388ada0b Mon Sep 17 00:00:00 2001 From: GRayHook Date: Mon, 28 Jul 2025 15:47:44 +0700 Subject: [PATCH] Init --- .gitignore | 2 ++ Makefile | 8 ++++++++ main.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e79b4d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +.*.swp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6d59d74 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +run: wrappers + ./wrappers + +wrappers: main.c + g++ -Wall -o wrappers main.c + +clean: + rm -f *.o wrappers diff --git a/main.c b/main.c new file mode 100644 index 0000000..f4a8e19 --- /dev/null +++ b/main.c @@ -0,0 +1,50 @@ +#include + +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 +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 w(&c); + + w->draw(); + + render_shape_by_ptr(w); + + render_shape_by_ref(w); +}