Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this call by reference create a new instance?

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;
};
like image 391
atamanroman Avatar asked Sep 20 '11 13:09

atamanroman


People also ask

What is the reason for using Call by reference?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

What happens when an argument is passed by reference?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference.

Why do we use Call by reference in java?

Example - Call By ReferenceIt creates a copy of references and passes them as valuable to the methods. As reference points to same address of object, creating a copy of reference is of no harm. But if new object is assigned to reference it will not be reflected.

What is Call by reference in Python?

It is a way of passing arguments to a function call in which both the actual argument and formal parameters refer to the same memory locations and any changes made within the function are reflected in the actual parameters of the function when called.


1 Answers

[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.

like image 139
Mark B Avatar answered Oct 27 '22 06:10

Mark B