foo
我通过以下方式调用方法const ref
:
// method, which is being called
void foo(const Entity & ent);
// call
Entity* e = new Entity;
foo(e); // wrong: missing * but compiles
这段代码不仅编译,它创建一个新实例,Entity
其默认值在foo
. 我希望这不会编译或至少崩溃。
如果我foo
正确调用 ( foo(*e)
),一切都会正常工作,并且我会看到Entity
within的正确值foo
。
我使用 Qt 4.7 提供的 mingw。
这是 的界面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;
};