0

where can I learn how "const" works? Any good tutorials you can recommend?

I'm not starting from scratch here but there are some things I'd like to repeat and some I still don't understand. E. g.:

MyClass myFunction(int number) const {
    // ...
}

What does const mean in this context?

chris342423
  • 439
  • 1
  • 6
  • 22
  • 5
    Pick a good book from [this list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1). – jrok Feb 14 '14 at 14:56
  • Any intro to C++ book will cover const. – Luchian Grigore Feb 14 '14 at 14:56
  • 1
    it means you can't change the member field of the class. `const` is used for the implicit `this` pointer – Jay Feb 14 '14 at 14:57
  • in C++11 the meaning of `const` is slightly different, but in all the previous versions of the language was the same, you should specify the version that you are using. – user2485710 Feb 14 '14 at 14:57
  • 1
    You would use this for member function (i.e., method) declarations/definitions. It declares that the member function/method won't touch instance variables. It's also a very useful piece of information for you, the programmer. If a method is const, you can be reasonably sure that the internal state of your object won't change after calling that method (unless some black magic with pointers is taking place behind the curtain). – Petr Skocik Feb 14 '14 at 14:57
  • @juanchopanza like @jrok answer, this answer is wrong too, without knowing the version of the language that the OP is using you can't possibly suggest the right definition of `const`. That's really annoying, for all the questions that I wrote for this website, there were always a bunch of people assuming things that weren't even there. Stop assuming. – user2485710 Feb 14 '14 at 15:05
  • @user2485710 If you think you have a better answer, you can vote to re-open and/or provide a relevant link. I really don't think this has changes so much between C++03 and C++11. The standard library is allowed to make some assumptions when running `const` methods in multi-threaded environments. Or is there something else? – juanchopanza Feb 14 '14 at 15:13
  • @juanchopanza I don't think that it's up to personal opinions to define "how much is too much", if something changes in terms of how it's defined in the standard, than it's considered changed, and using `const` in a C++03 implementation is different from C++11 compliant implementations. That really has nothing to do with the mechanism that govern this website or your personal opinion. – user2485710 Feb 14 '14 at 15:17
  • http://isocpp.org/blog/2012/12/you-dont-know-const-and-mutable-herb-sutter for the OP and who is interested – user2485710 Feb 14 '14 at 15:18
  • @user2485710 I think that is a misleading presentation. It suggests that the meaning of two keywords has changed, where in fact it hasn't. It just means they can be used in different ways, because C++11 has a concept of multiple threads. – juanchopanza Feb 14 '14 at 15:24
  • Here's a relevant question: http://stackoverflow.com/questions/14127379/does-const-mean-thread-safe-in-c11 – juanchopanza Feb 14 '14 at 15:27

0 Answers0