I'm changing an old routine that used to take an integer parameter so that it now takes a const reference to an object. I was hoping that the compiler would tell me where the function is called from (because the parameter type is wrong), but the object has a constructor that takes an integer, so rather than failing, the compiler creates a temporary object, passing it the integer, and passes a reference to that to the routine. Sample code:
class thing {
public:
thing( int x ) {
printf( "Creating a thing(%d)\n", x );
}
};
class X {
public:
X( const thing &t ) {
printf( "Creating an X from a thing\n" );
}
};
int main( int, char ** ) {
thing a_thing( 5 );
X an_x( 6 );
return 1;
}
I want the X an_x( 6 )
line to not compile, because there is no X
constructor that takes an int
. But it does compile, and the output looks like:
Creating a thing(5)
Creating a thing(6)
Creating an X from a thing
How can I keep the thing( int )
constructor, but disallow the temporary object?