const-correctness

Casting const void* to const int*

限于喜欢 提交于 2021-02-10 15:54:09
问题 I haven't used void* and const_correctness before so I am not understanding what I am doing wrong in the below code. All I want is to cast a void* returned by a member function of a const object to int*. Please suggest better approaches. Thank you. I get the following error passing 'const MyClass' as 'this' argument of 'void* MyClass::getArr()' discards qualifiers So here's the actual program that I had problem with class MyClassImpl{ CvMat* arr; public: MyClassImpl(){arr = new CvMat[10];}

Casting const void* to const int*

﹥>﹥吖頭↗ 提交于 2021-02-10 15:53:40
问题 I haven't used void* and const_correctness before so I am not understanding what I am doing wrong in the below code. All I want is to cast a void* returned by a member function of a const object to int*. Please suggest better approaches. Thank you. I get the following error passing 'const MyClass' as 'this' argument of 'void* MyClass::getArr()' discards qualifiers So here's the actual program that I had problem with class MyClassImpl{ CvMat* arr; public: MyClassImpl(){arr = new CvMat[10];}

Should I make my local variables const or movable?

纵然是瞬间 提交于 2021-02-06 15:26:27
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Should I make my local variables const or movable?

我们两清 提交于 2021-02-06 15:25:12
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Should I make my local variables const or movable?

本小妞迷上赌 提交于 2021-02-06 15:24:20
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Copy constructor from non-const argument suggested by thread-safety rules?

淺唱寂寞╮ 提交于 2020-02-21 10:29:14
问题 This bounty has ended . Answers to this question are eligible for a +400 reputation bounty. Bounty grace period ends in 23 hours . alfC is looking for an answer from a reputable source : “A well argumented answer or a reference to an existing good practice will be awarded.” I have a wrapper to some piece of legacy code. class A{ L* impl_; // the legacy object has to be in the heap, could be also unique_ptr A(A const&) = delete; L* duplicate(){L* ret; legacy_duplicate(impl_, &L); return ret;}

Copy constructor from non-const argument suggested by thread-safety rules?

做~自己de王妃 提交于 2020-02-21 10:25:32
问题 This bounty has ended . Answers to this question are eligible for a +400 reputation bounty. Bounty grace period ends in 23 hours . alfC is looking for an answer from a reputable source : “A well argumented answer or a reference to an existing good practice will be awarded.” I have a wrapper to some piece of legacy code. class A{ L* impl_; // the legacy object has to be in the heap, could be also unique_ptr A(A const&) = delete; L* duplicate(){L* ret; legacy_duplicate(impl_, &L); return ret;}

Is it legal/safe to cast away `const` for a heap-allocated object?

旧街凉风 提交于 2020-01-30 02:31:38
问题 My use case is as follows. I develop a library in which some loaded plugins can create objects (allocated using malloc() by the library), and some other plugins can read properties of those objects but not modify them. For me this is a case of having a non- const API for the creating/writer side and a const API for the reader side, for example: // writer API struct obj *obj_create(void); void obj_set_some_property(struct obj *obj, int property); // reader API int obj_get_some_property(const

Is it legal/safe to cast away `const` for a heap-allocated object?

淺唱寂寞╮ 提交于 2020-01-30 02:30:11
问题 My use case is as follows. I develop a library in which some loaded plugins can create objects (allocated using malloc() by the library), and some other plugins can read properties of those objects but not modify them. For me this is a case of having a non- const API for the creating/writer side and a const API for the reader side, for example: // writer API struct obj *obj_create(void); void obj_set_some_property(struct obj *obj, int property); // reader API int obj_get_some_property(const

Turning vector of shared_ptr into vector of shared_ptr to const

家住魔仙堡 提交于 2020-01-24 03:05:35
问题 Let class A { std::vector<std::shared_ptr<int>> v_; }; Now I'd like to add access to v_ using two public member functions std::vector<std::shared_ptr<int>> const & v() { return v_; } and std::vector<std::shared_ptr<int const> const & v() const { TODO } I cannot replace TODO with return v_; though. One option would be to not return a reference but a copy. Apart from the obvious performance penalty, this would also make the interface somewhat less desirable. Another option is to make TODO equal