Im calling a method foo
by const ref
:
// method, which is being called
void foo(const Entity & ent);
// call
Entity* e = new Entity;
foo(e); // wrong: missing * but compiles
This piece of code does not only compile, it creates a new instance of Entity
with its default values in the scope of foo
. I'd expect that this does not compile or at least crashes.
If I call foo
correctly (foo(*e)
), everything works as suspected and I see the right values of Entity
within foo
.
Im using mingw supplied with Qt 4.7.
Here's the interface of Entity
:
class Entity : public QObject
{
Q_OBJECT
public:
Entity (QObject* parent = NULL);
long getId() const { return this->id; }
void setId(const long id) { this->id = id; }
QString getName() const { return this->name; }
void setName(const QString & name) {this->name = name; }
private:
QString name;
long id;
};
[Edited] You have an implicit converting constructor (that also happens to be a default constructor) from Entity*
(via parent QObject*
) to Entity
and it's being used to create a temporary instance to pass in.
For this reason I always suggest by default making all single-parameter callable constructors (for example where all but one parameter is defaulted) explicit and avoiding implicit conversion operators except when they exactly perform the conversion semantics that would be expected in all circumstances. In other cases make the conversions available through explicit methods.
There are occasionally cases where implicit conversions are useful, and each one should be evaluated on a case-by-case basis.