Class constref
{
Public:
Constref (int ii );
PRIVATE:
Int I;
Const int ci;
Int & Ri;
};
Constref: constref (int ii)
{
I = II;
Ci = II;
Ri = I;
}
The usage of this constructor is obviously incorrect.
CI is of the const type and can only be initialized and cannot be assigned a value. The same rI = I; statement is also incorrect.
Result: the only opportunity for initializing a const or a referenced data member is in the constructor's initialization list.
The correct syntax is as follows:
Constref: constref (int ii): I (ii), CI (II), RI (I ){}
The test procedure is as follows:
# Include <iostream> <br/> using namespace STD; <br/> class constref <br/>{< br/> Public: <br/> constref (int ii ); <br/> void putout (); <br/> PRIVATE: <br/> int I; <br/> const int ci; <br/> Int & Ri; <br/>}; <br/> constref: constref (int ii): I (ii), CI (II), RI (I) {}< br/> void constref: putout () <br/>{ <br/> cout <I <Endl <CI <Endl <RI <Endl; <br/>}</P> <p> int main () <br/>{< br/> constref xxoo (3); <br/> xxoo. putout (); <br/> return exit_success; <br/>}