Thread: Problem With Const Correctness

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Problem With Const Correctness

    Hello, I am having this problem which keeps reoccurring during programming.
    In a class, for example, FloatArray I want to make a function
    Code:
    float average(int from, int to);
    that to the user, doesn't change the class, but the function might actually change private parts of the object, to make it more efficient.
    In this case, I want to save a list of the last 10 averages that user calculated, so that if the user asks for the same average, it wont need to be recalculated.
    The problem is that I can't make average() const, because it changes a (hidden) list inside of the object. Is there any way to use const and keep the benefits of const in this case?
    Whenever I have this problem, I usually just give up on const and that recursively forces me to make everything non-const. What should I do?
    Thanks for the help!

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    26
    Look here for example:
    mutable

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It is appropriate to use mutable when nothing changes as far as the external user of the class is concerned.

    e.g. Should there be a way for the external class to read the value of this mutable variable before and after it is changed during a const-operation, then using mutable would be wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Thanks a lot! I remember when I just began programming I heard about this, but they told me that it was bad programming practice to use it. I guess it was meant for this type of thing. Thanks again for the help

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by purestr999 View Post
    Thanks a lot! I remember when I just began programming I heard about this, but they told me that it was bad programming practice to use it. I guess it was meant for this type of thing. Thanks again for the help
    Because it is easy to overuse them. They are mostly used to create a transparent layer between the actual object's state and the user (result caching, debug logging, etc).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const correctness gone wrong I suspect...
    By matwsij in forum C++ Programming
    Replies: 9
    Last Post: 06-27-2011, 03:15 AM
  2. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  3. deep const correctness
    By robwhit in forum C Programming
    Replies: 20
    Last Post: 12-12-2008, 08:51 PM
  4. const correctness and static member variables
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2008, 05:26 PM