Does temporary object creation depends on the compiler?
In the code bellow, I call a function sending a char* but the function requires an Object reference. However, Object has a constructor that uses a char*.. So a temporary object is automatically created and send to the print function.
class Object {
string text;
public:
Object ( const char* value ) { text = value; }
void print() const { printf( "[%s]\n", text.c_str() ); }
};
void print( const Object& obj ) { obj.print(); }
int main() {
print( "hello" );
}
Does this behavior depends on the compiler?
You can see the output here: http://codepad.org/AABw5Ulz
When you declare such a constructor
it means the compiler can do an implicit conversion.
So no it is not a compiler dependent feature but it is by desing.
The compiler is simply allowed to take your
"hello"and pass it to theObjectcontructor before sending it to theprint(const Object &obj)function.