KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
( 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: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1995/N0798.htm How to deal with initialization of non-const reference member in const object? C++, Classes, Const, and strange syntax
Tags (comma-separated)
Save Edits
Cancel