Errors, bugs, questions - page 1706

 
Комбинатор:

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*'

Yes, I already wrote about it above. I modified the second sample but it still does not work. The operator returns a value, i.e. a copy. All three of us must have got so obtuse, eh! ))
 
Sergei Vladimirov:
All three of us had to be so dumb, eh! ))
So why does the first piece of the fxsaber example work then?
 
Комбинатор:
So why does the first piece of fxsaber example work then?

Why shouldn't he? He has an expression this[0] that returns a pointer. Here is the same thing in a different form:

int a;

int* Func(int& i)
{
   return(&i);
}

int* p = Func(a);   // работает, то же что A* a = this[0]
Func(a) = new int;  // не работает, то же, что и this[0] = new int
 
Sergei Vladimirov:
this[0] is rvalue here.
Exactly not like this
class A
{
public:    
  int Tmp;
  
  void operator =( A* )
  {
  }

  void operator =( const int Value )
  {
    this.Tmp = Value;
  }
};

class B
{
public:  
  A* Data[];
  
  B()
  {
    ArrayResize(this.Data, 1);
  }

  A* operator []( const int Pos )
  {
    return(this.Data[Pos]);
  }
  
  void Init()
  {
    // this[0] = new A; // ошибка выполнения: invalid pointer access
    this.Data[0] = new A;
    
    this[0] = 5; // this[0] - НЕ rvalue
  } 
};

void OnStart()
{
  B b;
  
  b.Init();
  
  Print(b[0].Tmp);
}


Result
2016.09.25 18:57:42.214 Test (RTS-12.16,M1)     5
The problem is some nonsense.
void A::operator =( A* )
Unfortunately, the vocabulary is scarce to explain it.
 
The problem is that the pointer (descriptor) from rvalue somehow turns into lvalue after being assigned to a variable. It's a bit crazy...
 

fxsaber:
Точно не так

Результат

The problem is a certain pointlessness

Unfortunately, the vocabulary is scarce for explanation.

OK, I don't feel like thinking. Tired already.

 
Комбинатор:
The problem is that the pointer (descriptor) from rvalue somehow turns into lvalue after being assigned to a variable. That's a bit crazy...
Yes it's me who is confused. After
    A* a = this[0];
    a = new A; // так работает

The expression

CheckPointer(this[0]) == POINTER_INVALID

will be true. Which makes perfect sense.

The developers should formulate the reason why it is impossible clearly and nicely

this[0] = new A;
 
fxsaber:
Yeah, I messed up.
Yeah, I got confused. I should have checked before I jumped the gun. Anyway, it's all good :)
 
Комбинатор:
In short, it's all good :)
I still don't understand the last example. Overloading "=" operator in class A turns this[0] from rvalue into lvalue... I can't figure out how. Is this some kind of MCL feature, there's no such behaviour in the pros.fxsaber, how did you even get to this construct? I mean overload = in A. Or by trial and error?
 
The pointer is assigned an rvalue, which is then overwritten by new A.
Reason: