Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is conversion from const pointer-to-const to const pointer-to-nonconst in an initializer list allowed
    primarykey
    data
    text
    <p>I read the question posted on <a href="https://stackoverflow.com/questions/6936124/why-does-c-not-have-a-const-constructor">Why does C++ not have a const constructor?</a></p> <p>I am still confused why that program can compile. And I tried to offer my opinion on the question, I don't know why it was deleted. So I have to ask the question again.</p> <p>Here is the program</p> <pre><code>class Cheater { public: Cheater(int avalue) : value(avalue), cheaterPtr(this) //conceptually odd legality in const Cheater ctor {} Cheater&amp; 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; } </code></pre> <p>And my confusion is :</p> <p><code>const Cheater cheater(7)</code> creates a const object cheater, in its constructor</p> <pre><code> Cheater(int avalue) : value(avalue), cheaterPtr(this) //conceptually odd legality in const Cheater ctor {} </code></pre> <p>'this' pointer was used to initialize <code>cheaterPtr</code>.</p> <p>I think it shouldn't be right. <code>cheater</code> is a const object, whose this pointer should be something like: <code>const Cheater* const this;</code> which means the pointer it self and the object the pointer points to should both const, we can neither change the value of the pointer or modify the object the pointer points to.</p> <p>but object <code>cheater</code>'s <code>cheaterPtr</code> member is something like <code>Cheater* const cheaterPtr</code>. Which means the pointer is const but the object it points to can be nonconst. </p> <p>As we know, pointer-to-const to pointer-to-nonconst conversion is not allowed:</p> <pre><code>int i = 0; const int* ptrToConst = &amp;i; int * const constPtr = ptrToConst; // illegal. invalid conversion from 'const int*' to 'int*' </code></pre> <p>How can conversion from pointer-to-const to pointer-to-nonconst be allowed in the initializer list? What really happened?</p> <p>And here is a discription about "constness" in constructors I tried to offer to the original post:</p> <p>"Unlike other member functions, constructors may not be declared as const. When we create a const object of a class type, the object does not assume its 'constness' until after the constructor completes the object's initialization. Thus, constructors can write to const objects during their construction."</p> <p>--C++ Primer (5th Edition) P262 7.1.4 Constructors</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload