What is the difference between const and readonly?
What is the difference between const and readonly and do you use one over the other? ...
What is the difference between const and readonly and do you use one over the other? ...
Hi everyone. I'm having a little trouble figuring out exactly how const applies in a specific case. Here's the code I have: struct Widget { Widget():x(0), y(0), z(0){} int x, y, z; }; struct WidgetHolder //Just a simple struct to hold four Widgets. { WidgetHolder(Widget a, Widget b, Widget c, Widget d): A(a), B(b), C(c), D...
The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc & g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care): char **a; const char** b = a; I can understand and accept this. The C++ solution to this problem is to change b to be a const char * const *, which disallows ...
My first question on Stack Overflow... :-) Someone told me about a C++ style difference in their team. I have my own viewpoint on the subject, but I would be interested by pros and cons coming from everyone. So... In case you have a class property you want to expose via two getters, one read/write, and the other, readonly (i.e. there i...
I understand that the function is not allowed to change the state of the object, but I thought I read somewhere that the compiler was allowed to assume that if the function was called with the same arguments, it would return the same value and thus could reuse a cached value if it was available. e.g. class object { int get_value(int...
How far do you go with const? Do you just make functions const when necessary or do you go the whole hog and use it everywhere? For example, imagine a simple mutator that takes a single boolean parameter: void SetValue(const bool b) { my_val_ = b; } Is that const actually useful? Personally I opt to use it extensively, including pa...
Let's say I have the following class X where I want to return access to an internal member: class Z { // details }; class X { std::vector<Z> vecZ; public: Z& Z(size_t index) { // ... // massive amounts of code for // validating index Z& ret = vecZ[index]; // even more code for ...
If not, what's the common practice for specifying variables that are used as constants? ...
Whilst refactoring some code I came across some getter methods that returns a std::string. Something like this for example: class foo { private: std::string name_; public: std::string name() { return name_; } }; Surely the getter would be better returning a const std::string&? The current method is returning ...
Quick question: int testfunc1 (const int a) { return a; } int testfunc2 (int const a) { return a; } Are these two functions the same in every aspect or is there a difference? I'm interested in an answer for the C-language, but if there is something interested in the C++ case I'd like to know as well. ...
For example, suppose I have a class: class Foo { public: std::string& Name() { m_maybe_modified = true; return m_name; } const std::string& Name() const { return m_name; } protected: std::string m_name; bool m_maybe_modified; }; And somewhere else in the code, I have something l...
Consider these two C++ header cases: Case 1: class Test { public: static int TEST_DATA[]; }; int Test::TEST_DATA[] = { 1, 2, 3, 4 }; Case 2: class Test { public: static int const TEST_DATA[]; }; int const Test::TEST_DATA[] = { 1, 2, 3, 4 }; Is const in the latter case only for self-imposed compile-time checks or does i...
After some find and replace refactoring I ended up with this gem: const class A { }; What does "const class" mean? It seems to compile ok. ...
Is it right to use a private constant in the following situation: Say I have a game with a lives variable and a startingLives variable. At the start of the game I set the lives variable to equal the startingLives variable. This is how I would normally do it: private var lives:int = 0; private var startingLives:int = 3; private functio...
I've often used pointers to const objects, like so... const int *p; That simply means that you can't change the integer that p is pointing at through p. But I've also seen reference to const pointers, declared like this... int* const p; As I understand it, that means that the pointer variable itself is constant -- you can change th...
private const int THE_ANSWER = 42; or private const int theAnswer = 42; Personally I think with modern IDEs we should go with camelCase as ALL_CAPS smells "Hungarian". What do you think? ...
Hi, In the below code snippet can i replace char * to const char * and remove the strdup() function call and directly take the optarg value set by getopt()? I am advised to use const char * to skip the strdup function usage. Appreciate the help in advance. /* Code Snippet */ char *dir = NULL; char *bld = NULL; int chr; while ( ( chr ...
In C++, a function's signature depends partly on whether or not it's const. This means that a class can have two member functions with identical signatures except that one is const and the other is not. If you have a class like this, then the compiler will decide which function to call based on the object you call it on: if it's a cons...
This declaration causes an overflow in VBA: Const OVERFLOWS As Long = 10 * 60 * 60 whereas setting the value directly is fine: Const COMPILES_OK As Long = 36000 How do you persuade VBA to treat literal integers as longs? Thanks ...
The following code says that passing the map as const into the operator[] method discards qualifiers: #include <iostream> #include <map> #include <string> using namespace std; class MapWrapper { public: const int &get_value(const int &key) const { return _map[key]; } private: map<int, int> _map; }; int main() { ...