Hi,
I have came across with following constructs a lot. But I couldn't give them a meaning. Please help me understand them.
Suppose we have function and and gets a reference to a QString parameter. In order to call that function one has to create a QString object and pass it to the function.Qt Code:
void afunction (QString& str) {...};To copy to clipboard, switch view to plain text mode
But the weird thing is you don't have to create an object. You can just pass a string literal like "This is a Literal" to the function instead of a QString object;Qt Code:
afunction(obj);To copy to clipboard, switch view to plain text mode
and it works. Why?Qt Code:
afunction("This is a Literal");To copy to clipboard, switch view to plain text mode
Apart from sending a string literal to the above function there is also another weird way to call the function.
Above call works but it shouldn't. How an QString object gets created just by using class name with parantheses? It is not even creates an object. What is the meaning of QString("Hi there") alone in the function parameter list? First of all it is not the way books thought us creating objects. I can create a QString object with two ways that I know.Qt Code:
To copy to clipboard, switch view to plain text mode
I don't know any other way to create an object. Turning back to original subject, How an object gets created just by using it's class name?Qt Code:
To copy to clipboard, switch view to plain text mode
regards
Bookmarks