Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 5 characters in body
Source Link
fredoverflow
  • 264.7k
  • 101
  • 403
  • 680

What's wrong with const is that many programmers don't seem to be able to understand it completely, and a "half-const-correct" project simply does not work. This is what you need to know:

  1. Foo vs. const Foo (or Foo const)
  2. Foo& vs. const Foo& (or Foo const&)
  • references-to-const bind to all kinds of things, while references-to-non-const don't
  1. Foo* vs. const Foo* (or Foo const*)
  • pointer variables can also be Foo* const and const Foo* const (or Foo const* const)
  1. void Foo::mutator() vs. void int Foo::accessor() const
  • but pointer members inside const member functions still point to non-const objects
  • so we can accidentally return non-const data from a const-function
  1. iterator vs. const_iterator
  • iterator variables can also be const iterator and const const_iterator

Migrating to C++ from a language that has no const concept is quite hard, any many fail to see the point.

What's wrong with const is that many programmers don't seem to be able to understand it completely, and a "half-const-correct" project simply does not work. This is what you need to know:

  1. Foo vs. const Foo (or Foo const)
  2. Foo& vs. const Foo& (or Foo const&)
  • references-to-const bind to all kinds of things, while references-to-non-const don't
  1. Foo* vs. const Foo* (or Foo const*)
  • pointer variables can also be Foo* const and const Foo* const (or Foo const* const)
  1. void Foo::mutator() vs. void int Foo::accessor() const
  • but pointer members inside const member functions still point to non-const objects
  • so we can accidentally return non-const data from a const-function
  1. iterator vs. const_iterator
  • iterator variables can also be const iterator and const const_iterator

Migrating to C++ from a language that has no const concept is quite hard, any many fail to see the point.

What's wrong with const is that many programmers don't seem to be able to understand it completely, and a "half-const-correct" project simply does not work. This is what you need to know:

  1. Foo vs. const Foo (or Foo const)
  2. Foo& vs. const Foo& (or Foo const&)
  • references-to-const bind to all kinds of things, while references-to-non-const don't
  1. Foo* vs. const Foo* (or Foo const*)
  • pointer variables can also be Foo* const and const Foo* const (or Foo const* const)
  1. void Foo::mutator() vs. int Foo::accessor() const
  • but pointer members inside const member functions still point to non-const objects
  • so we can accidentally return non-const data from a const-function
  1. iterator vs. const_iterator
  • iterator variables can also be const iterator and const const_iterator

Migrating to C++ from a language that has no const concept is quite hard, any many fail to see the point.

added 25 characters in body; added 3 characters in body
Source Link
fredoverflow
  • 264.7k
  • 101
  • 403
  • 680

What's wrong with const is that many programmers don't seem to be able to understand it completely, and a "half-const-correct" project simply does not work. This is what you need to know:

  1. Objects can be constFoo vs. const Foo (or Foo const)
  2. References to objects can be constFoo& vs. const Foo& (or Foo const&)
  • references-to-const bind to all kinds of things, while normal references-to-non-const don't
  1. Pointers can be constFoo* vs. const Foo* (or Foo const*)
  • with pointer variables, we have four possible combinations of const before can also be Foo* const and after the starconst Foo* const (or Foo const* const)
  1. Member functions can be constvoid Foo::mutator() vs. (this I find quite useful)void int Foo::accessor() const
  • but pointer members inside const member functions still point to non-const objects
  • so we can accidentally return non-const data from a const-function
  1. If you have a constant view on a container, begin and end silently yield a const_iteratoriterator vs. const_iterator
  • not to be confused with a const iterator variables can also be const iterator and const const_iterator

Migrating to C++ from a language that has no const concept is quite hard, any many fail to see the point.

What's wrong with const is that many programmers don't seem to be able to understand it completely, and a "half-const-correct" project simply does not work. This is what you need to know:

  1. Objects can be const
  2. References to objects can be const
  • references-to-const bind to all kinds of things, while normal references don't
  1. Pointers can be const
  • with pointer variables, we have four possible combinations of const before and after the star
  1. Member functions can be const (this I find quite useful)
  • but pointer members inside const member functions still point to non-const objects
  • so we can accidentally return non-const data from a const-function
  1. If you have a constant view on a container, begin and end silently yield a const_iterator
  • not to be confused with a const iterator

Migrating to C++ from a language that has const concept is quite hard, any many fail to see the point.

What's wrong with const is that many programmers don't seem to be able to understand it completely, and a "half-const-correct" project simply does not work. This is what you need to know:

  1. Foo vs. const Foo (or Foo const)
  2. Foo& vs. const Foo& (or Foo const&)
  • references-to-const bind to all kinds of things, while references-to-non-const don't
  1. Foo* vs. const Foo* (or Foo const*)
  • pointer variables can also be Foo* const and const Foo* const (or Foo const* const)
  1. void Foo::mutator() vs. void int Foo::accessor() const
  • but pointer members inside const member functions still point to non-const objects
  • so we can accidentally return non-const data from a const-function
  1. iterator vs. const_iterator
  • iterator variables can also be const iterator and const const_iterator

Migrating to C++ from a language that has no const concept is quite hard, any many fail to see the point.

Source Link
fredoverflow
  • 264.7k
  • 101
  • 403
  • 680

What's wrong with const is that many programmers don't seem to be able to understand it completely, and a "half-const-correct" project simply does not work. This is what you need to know:

  1. Objects can be const
  2. References to objects can be const
  • references-to-const bind to all kinds of things, while normal references don't
  1. Pointers can be const
  • with pointer variables, we have four possible combinations of const before and after the star
  1. Member functions can be const (this I find quite useful)
  • but pointer members inside const member functions still point to non-const objects
  • so we can accidentally return non-const data from a const-function
  1. If you have a constant view on a container, begin and end silently yield a const_iterator
  • not to be confused with a const iterator

Migrating to C++ from a language that has const concept is quite hard, any many fail to see the point.