TITLE: what is const &X (Newsgroups: comp.lang.c++.moderated, 27 Nov 96) PROBLEM: Bart Lamiroy >I recently received some publicity for PC Lint++ from Gimpel Software. >It contained an example of vicious bugs a programmer may encounter for >which they offer a solution. I reproduced their example below without >their permission (fair use) : > >#include > >class X >{ >public: > X(const &X) { kind="copy"; } > X() { kind = "original";} > char* kind; >}; > >void f(X x) >{ cerr << x.kind << endl;} > >int main() >{ > X x; > f(x); > return 0; >} > > > >This code generates "original" at execution time and not "copy" for the >simple reason that NO copy constructor is defined but some hybrid >monster (note the `const &X' instead of `const X&') > >Now, my question is : What is the semantic meaning of this `const &X' ? RESPONSE: clamage@taumet.Eng.Sun.COM (Steve Clamage) It is an example of the "implicit int" inherited from C. The constructor definition is equivalent to X(const int& p) { kind = "copy"; } Just for fun, add the lines X y(1); cerr << "y.kind = " << y.kind << endl; to main, and watch the program print "y.kind = copy" (if your compiler accepts the code). The C++ draft standard removed "implicit int" as valid code, but some compilers continue to accept it. Eventually, all compilers will flag it as an error. That really is an insidious bug, and serves as an example of why "implicit int" was removed from C++. I hear that the C Committee is planning to remove implicit int from the next C standard.