diff --git a/include/rcp.h b/include/rcp.h index 9d33a6a..1d169bc 100644 --- a/include/rcp.h +++ b/include/rcp.h @@ -297,9 +297,9 @@ rcp rcp_dynamic_cast(rcp && other) #define rcp_managed(classname) \ public: \ - rcp get_rcp() \ + rcp get_rcp() const \ { \ - return rcp(this); \ + return rcp(const_cast(this)); \ } \ template \ static rcp create(Args&&... args) \ diff --git a/tests.cpp b/tests.cpp index a1d4df9..0f9d695 100644 --- a/tests.cpp +++ b/tests.cpp @@ -114,6 +114,16 @@ struct Receiver } }; +void test_get_rcp_const() +{ + auto a = MyObj::create(); + const MyObj & ref = *a; + rcp b = ref.get_rcp(); + assert(b); + assert(b->v == 42); + assert(b.use_count() == 2); +} + void test_multi_construct_from_raw_pointers() { Receiver r; @@ -423,6 +433,7 @@ int main(int argc, char * argv[]) test_dereference(); test_booleans(); test_create(); + test_get_rcp_const(); test_multi_construct_from_raw_pointers(); test_listener_self_registration(); test_copy_assignment_decrements_previous_reference();