When checking my code with PC-LINT it advised me to make some member-functions const
What is the advantage of doing this ?

Dani AI

Generated

As noted, PC‑LINT's suggestion to mark some member functions const is generally sound. Marking a member function const documents intent, lets the compiler enforce that the function does not modify the object's observable state, and allows the function to be called on const objects or references — all practical benefits of const‑correctness that , and touched on.

A common pattern where const is still appropriate even though some internal mutation happens is lazy caching. The cache fields are declared mutable so the accessor remains logically const:

class LazyValue {
    mutable bool cached_;
    mutable int cachedResult_;
public:
    LazyValue() : cached_(false), cachedResult_(0) {}
    int result() const {
        if (!cached_) {
            cachedResult_ = compute();
            cached_ = true;
        }
        return cachedResult_;
    }
private:
    int compute() const; // expensive calculation
};

Another useful idiom is pairing const and non‑const overloads so the same operation works on both const and non‑const objects:

class Buffer {
    std::vector<char> data_;
public:
    char& operator[](size_t i) { return data_[i]; }
    const char& operator[](size_t i) const { return data_[i]; }
};

Practical troubleshooting notes for PC‑LINT warnings: confirm the method truly doesn’t alter the object’s observable state; if changes are only caches or transient bookkeeping, use mutable or split the work into a non‑mutating accessor and a separate modifier. Be careful with virtual functions — constness is part of the signature and must match overrides. Avoid const_cast to silence warnings except in very controlled cases. Finally, remember const documents intent but does not imply thread‑safety or immutability of pointed‑to data. Applying const where appropriate improves API clarity and catches accidental writes at compile time.

Recommended Answers

All 4 Replies

I do it just to remind myself (and the compiler) that no class variables can be changed in this function. Basically just to keep me from accidentally doing something stupid!

ie.

double YourClass::GetX()
{
  x_ = 2.0;
}

You should not be modifying any of the member variables in a get function! But the compiler doesn't know that, unless you do this:

double YourClass::GetX() const
{
  x_ = 2.0;
}

Then it will give you an error, because you are not allowed to modify x_ from this function!

Hope that helps,

Dave

const is a 'promise' not to change.

If you have member-functions that do not change your class (and never should) you can declare them const.

const member-functions are declared like this:

int getValue() const;

The primary purpose for const member-functions is that they are the only member-functions you can call for a const reference to that class.

Functions that need to be able to look at a class but do not need to change it should declare the parameter as a const reference.

class Sample;
int myfunc(Sample const & data)
{
//...
}

Inside myfunc (above) the only member-functions of data that can be called are the const member-functions.

Daily in the morning, chant out this line " Its better to detect error at compile time than at link time or run time."
And this is the whole key to const-correctness, this is what you call it in technical terms.
Read:http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.1
const-correctness basically ensure type safety. It should be practiced by every efficient programmer. It can sometime saves you hour of work!!

Thank you all for the info...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.