Why is it required to return a reference from overloading bracket operators (OR: why is an lvalue not returned otherwise)?

StackOverflow https://stackoverflow.com/questions/16227783

Question

I've tried overloading the bracket operators for a class, to make accessing the array less tedious. What I don't understand is, why is it neccesary to declare the return type of the overload function as a reference? Why is it not an lvalue to begin with?

struct particle
{
    double v_x;
} 

struct particleSwarm
{
    int numParticles;
    particle* particles;
    particle operator[](int i) { return particles[i]; }
}

It returns a particle struct, so why is this not valid, unless I make the operator return a reference:

void foo(particleSwarm& swarm)
{
    swarm[0].v_x = 5.0;
}

What I don't understand is, why isn't the overloaded function already returning an lvalue? When trying to figure it out, I discovered that something like this is valid:

int* foo(particleSwarm* swarm)
{
    return &(swarm->numParticles);
}

void bar(particleSwarm* swarm)
{
    *(foo(swarm)) = 5;
}

Why is the pointer returned by foo a valid lvalue that can be dereferenced and assigned to, but not the object returned by the overload? I see how it wouldn't be if I were assigning directly to it, since I haven't overloaded =, but I'm assigning to one of it's member variables, which it seems like should be valid? I'm just having a hard time understanding, so I appreciate anyone who takes the time to help!

Was it helpful?

Solution

Your code

void foo(particleSwarm& swarm)
{
    swarm[0].v_x = 5.0;
}

will be compiled into something like

void foo(particleSwarm& swarm)
{
    particle tmp = swarm.operator[](0);
    tmp.v_x = 5.0;
    // destruct tmp
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top