(Edit: Heavy change because previous example was flawed, which may make some answers/comments seem odd)
This might be an overly contrived, but the following is legal because of lack of const constructor:
class Cheater
{
public:
Cheater(int avalue)
: cheaterPtr(this) //conceptually odd legality in const Cheater ctor
, value(avalue)
{}
Cheater& getCheaterPtr() const {return *cheaterPtr;}
int value;
private:
Cheater * cheaterPtr;
};
int main()
{
const Cheater cheater(7); //Initialize the value to 7
cheater.value = 4; //good, illegal
cheater.getCheaterPtr().value = 4; //oops, legal
return 0;
}
It seems like providing a const constructor a thing would be as easy technically as const methods, and be analogous to a const overload.
Note: I'm not looking for 'Image( const Data & data ) const' but rather 'const Image( const Data & data) const'
So:
- Why is the const constructor absent in C++?
Here's some related material for context: