What is the difference in const-correctness between C and C++?

In addition to the differences you cite, and the library differences that Steve Jessop mentions, char* p1; char const* const* p2 = &p1; is legal in C++, but not in C. Historically, this is because C originally allowed: char* p1; char const** p2 = &p1; Shortly before the standard was adopted, someone realized that this … Read more

What is meaning of a pointer to a constant function?

In C, there’s no such thing as a function being const or otherwise, so a pointer to a const function is meaningless (shouldn’t compile, though I haven’t checked with any particular compiler). Note that although it’s different, you can have a const pointer to a function, a pointer to function returning const, etc. Essentially everything … Read more

Does using const on function parameters have any effect? Why does it not affect the function signature?

const is pointless when the argument is passed by value since you will not be modifying the caller’s object. Wrong. It’s about self-documenting your code and your assumptions. If your code has many people working on it and your functions are non-trivial then you should mark const any and everything that you can. When writing … Read more

error: passing const xxx as ‘this’ argument of member function discards qualifiers

The objects in the std::set are stored as const StudentT. So when you try to call getId() with the const object the compiler detects a problem, mainly you’re calling a non-const member function on const object which is not allowed because non-const member functions make NO PROMISE not to modify the object; so the compiler … Read more

Why can’t a static member function have a const qualifier?

When you apply the const qualifier to a nonstatic member function, it affects the this pointer. For a const-qualified member function of class C, the this pointer is of type C const*, whereas for a member function that is not const-qualified, the this pointer is of type C*. A static member function does not have … Read more

Should you use const in function parameters, and why does it not affect the function signature?

const is pointless when the argument is passed by value since you will not be modifying the caller’s object. Wrong. It’s about self-documenting your code and your assumptions. If your code has many people working on it and your functions are non-trivial then you should mark const any and everything that you can. When writing … Read more