Errors, bugs, questions - page 1705

 
fxsaber:
Stupid. Can you tell me why you can't do that?
Got it.
 
fxsaber:
Got it.
Explain
 
Комбинатор:
Explain
It won't work - there's a terminology gap.
 
Комбинатор:
Explain
this[0] here is rvalue.
 
Sergei Vladimirov:
here[0] is rvalue.

This is what is not clear.

If the assignment works in the first case, logically it should work in the second one as well. Because in the first case the same rvalue is assigned to the pointer, but the operator then works!

This is just my opinion, of course. But I want to make it out.

 
Комбинатор:

That's what I don't understand.

I don't understand it either. )) Upd. Already got it.

If assignment works in the first case, it should logically work in the second one as well. That's because in the first case, the same rvalue is assigned to the pointer but the operator works after that!

This is, of course, just my opinion. But I want to make it out.

rvalue can only be assigned to a left-handed value or be a temporary constant.

This is what works in the pluses:

class A
{
public:
        int m[10];

        int& operator [] (const int i)
        {
                return((m[i]));
        }
};

int main()
{
        A oA;
        oA[2] = 5;
}

This one doesn't.

class A
{
public:
        int* m[10];

        int* operator [] (const int i)
        {
                return((m[i]));
        }
};

int main()
{
        A oA;
        oA[2] = new int;    // error C2106: =: левый операнд должен быть левосторонним значением
}
 
Sergei Vladimirov:

This one isn't, and I can't figure out the difference:

If you add a link to the pointer it will work. Or a double pointer.
 
Комбинатор:
If you add a link to the pointer it will work. Or a double pointer.
I don't understand, what link, where?
 
Ugh, man. In the second example operator[] returns address of the i-th element - of course, it cannot be changed. Stupid in the evening.
 

Oops, I'm slowing down, the second example isn't right at all.

That's why it is logical that

error: invalid initialization of non-const reference of type 'int*&' from an rvalue of type 'int*'