From c56d7fe0eba7905b0a63c4a1cfe909988653c23d Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 27 Oct 2015 02:51:43 -0400 Subject: Add DISABLE_CLASS_COPY macro (and use it) Use this macro to disallow copying of an object using the assignment operator or copy constructor. This catches otherwise silent-but-deadly mistakes such as "ServerMap map = env->getMap();" at compile time. If so desired, it is still possible to copy a class, but it now requires an explicit call to memcpy or std::copy. --- src/basicmacros.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/basicmacros.h') diff --git a/src/basicmacros.h b/src/basicmacros.h index 05987e32f..cebf06043 100644 --- a/src/basicmacros.h +++ b/src/basicmacros.h @@ -30,4 +30,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #define CONTAINS(c, v) (std::find((c).begin(), (c).end(), (v)) != (c).end()) +// To disable copy constructors and assignment operations for some class +// 'Foobar', add the macro DISABLE_CLASS_COPY(Foobar) as a private member. +// Note this also disables copying for any classes derived from 'Foobar' as well +// as classes having a 'Foobar' member. +#define DISABLE_CLASS_COPY(C) \ + C(const C &); \ + C &operator=(const C &) + #endif -- cgit v1.2.3