const-correctness

Turning vector of shared_ptr into vector of shared_ptr to const

被刻印的时光 ゝ 提交于 2020-01-24 03:04:37
问题 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

Turning vector of shared_ptr into vector of shared_ptr to const

纵然是瞬间 提交于 2020-01-24 03:04:24
问题 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

Why is my return type meaningless?

落花浮王杯 提交于 2020-01-21 06:33:07
问题 I am trying to use a return type of const MyClass * const . However, I get a warning: Warning: #815-D: type qualifier on return type is meaningless. Is this not a valid type? I want a pointer than cannot be changed, and I want the thing it points to to not be changed either. 回答1: The pointer itself has value type, so it doesn't make sense to make it const. What the caller function does with the returned value can't be restricted by the called function. This is akin to trying to define

Const-correct Notifier in Observer Pattern

∥☆過路亽.° 提交于 2020-01-15 06:39:22
问题 I want to implement an Observer of a Model class which does not change the Model. Thus, it should be able to use a const-Reference to access the Model. But the Registering of the Observer prohibits this. Here is how the observer pattern is implemented in my Project: //Attributes of type Observable are used by classes that want to notify others //of state changes. Observing Objects register themselves with AddObserver. //The Observable Object calls NotifyObservers when necessary. class

C++ Pass By Const Reference and Return By Const Reference

泄露秘密 提交于 2020-01-11 04:35:10
问题 I'm trying to understand if there is any benefit to returning a const reference. I have a factorial function that normally looks like this: unsigned long factorial(unsigned long n) { return (n == 0) ? 1 : n * factorial(n - 1); } I'm assuming that there will be a performance increase when we pass by const reference and we return a const reference... but const -correctness always confuses me. const unsigned long & factorial(const unsigned long& n) { return (n == 0) ? 1 : n * factorial(n - 1); }

const correctness and return values - C++

瘦欲@ 提交于 2020-01-06 15:50:54
问题 Please consider the following code. struct foo { }; template<typename T> class test { public: test() {} const T& value() const { return f; } private: T f; }; int main() { const test<foo*> t; foo* f = t.value(); return 0; } t is a const variable and value() is a constant member-function which returns const T& . AFAIK, a const type is not assignable to a non-const type. But how foo* f = t.value(); compiles well. How this is happening and how can I ensure value() can be only assigned to const

C++ Preventing const methods from changing data through a member pointer or reference

孤人 提交于 2020-01-01 04:24:09
问题 Say I have a simple class like this class Foo { public: void foo()const { str[5] = 'x'; obj->changeTheWorld(); x = 4; y.get() = 5; obj2->changeTheWorld(); } private: char *str; //some referenced data, not owned by Foo ComplexObj *obj; //some referenced data, not owned by Foo int &x; //references as well //wrapped reference, but has a "T& get()const" std::reference_wrapper<int> y; //an occasionally useful pointer wrapper for complex memory cases //but has a "T* get()const" std::shared_ptr

std::vector of objects and const-correctness

孤者浪人 提交于 2019-12-31 12:50:58
问题 Consider the following: class A { public: const int c; // must not be modified! A(int _c) : c(_c) { // Nothing here } A(const A& copy) : c(copy.c) { // Nothing here } }; int main(int argc, char *argv[]) { A foo(1337); vector<A> vec; vec.push_back(foo); // <-- compile error! return 0; } Obviously, the copy constructor is not enough. What am I missing? EDIT: Ofc. I cannot change this->c in operator=() method, so I don't see how operator=() would be used (although required by std::vector). 回答1:

Idiomatic Way to declare C++ Immutable Classes

独自空忆成欢 提交于 2019-12-28 03:45:18
问题 So I have some pretty extensive functional code where the main data type is immutable structs/classes. The way I have been declaring immutability is "practically immutable" by making member variables and any methods const. struct RockSolid { const float x; const float y; float MakeHarderConcrete() const { return x + y; } } Is this actually the way "we should do it" in C++? Or is there a better way? 回答1: The way you proposed is perfectly fine, except if in your code you need to make assignment

“const correctness” in C#

若如初见. 提交于 2019-12-27 13:38:25
问题 The point of const-correctness is to be able to provide a view of an instance that can't be altered or deleted by the user. The compiler supports this by pointing out when you break constness from within a const function, or try to use a non-const function of a const object. So without copying the const approach, is there a methodology I can use in C# that has the same ends? I'm aware of immutability, but that doesn't really carry over to container objects to name but one example. 回答1: I've