From b9255b1bfb7d6d00e8ce7362c7de4f02edc0617e Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 12 Mar 2021 17:33:10 -0600 Subject: [PATCH] Compiler warnings, and don't dynamic_cast twice in Entity::GetComponent --- examples/snake/src/main.cpp | 3 +-- include/simpleengine/entity.h | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/snake/src/main.cpp b/examples/snake/src/main.cpp index 246565b..4c68b50 100644 --- a/examples/snake/src/main.cpp +++ b/examples/snake/src/main.cpp @@ -248,8 +248,7 @@ public: } void OnCollide(std::shared_ptr entity) { - std::shared_ptr shared = dynamic_pointer_cast(entity); - if (shared) { + if (std::shared_ptr shared = std::dynamic_pointer_cast(entity); shared) { shared->Relocate(); BodyPiece piece{ sf::RectangleShape(sf::Vector2f(15, 15)), sf::Vector2i(0, 0) }; diff --git a/include/simpleengine/entity.h b/include/simpleengine/entity.h index c07fd2a..1c2616b 100644 --- a/include/simpleengine/entity.h +++ b/include/simpleengine/entity.h @@ -40,7 +40,7 @@ namespace simpleengine { template bool HasComponent() const { for (std::shared_ptr comp : components) { - if (dynamic_cast(comp.get())) { + if (std::dynamic_pointer_cast(comp)) { return true; } } @@ -51,8 +51,8 @@ namespace simpleengine { template std::shared_ptr GetComponent() const { for (std::shared_ptr comp : components) { - if (dynamic_cast(comp.get())) { - return dynamic_pointer_cast(comp); + if (std::shared_ptr dyn_comp = std::dynamic_pointer_cast(comp); dyn_comp) { + return dyn_comp; } }