Like Tree2Likes
  • 1 Post By iMalc
  • 1 Post By kmdv

Problem With Const Correctness

This is a discussion on Problem With Const Correctness within the C++ Programming forums, part of the General Programming Boards category; Hello, I am having this problem which keeps reoccurring during programming. In a class, for example, FloatArray I want to ...

  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
    24
    Look here for example:
    mutable

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    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.
    Caligulaminus likes this.
    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
    681
    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).
    Caligulaminus likes this.
    I never put signature, but I decided to make an exception.

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, 07:51 PM
  4. const correctness and static member variables
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2008, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21