Errors, bugs, questions - page 2356

 
A100:

in C++: A::f()

What does this code say?

((A*)&b).f();
 
fxsaber:

What does this code say?

It's the same.

 
Ilya Malev:

Same thing.

as MQL (assuming it is converted to C++ form). But you could have checked it yourself
 
A100:
That and MQL (assuming it is converted to C++ form). But you could have checked it yourself

Checked:


 
Ilya Malev:

Checked:

I think you are checking something wrong.

B*b1=a;

Such an entry in MQL means copying of an object a - to an object located by pointer b1. In general, of course, it should not compile.

And in order not to mix up objects and pointers, you'd better use the p prefix for pointers

 
Ilya Malev:

Checked:

Please don't be pawned off on this misguided behaviour.

There are 2 problems here(?):

  1. B *b1=a - an explicit error, the compiler perceives this construct as a call of the copy operator A::A(const A &)

  2. In the first case, the optimizer has detected that A::A(const A &) is empty, no members - no access by "pointer" (in fact, there is no code for calling the copy operator, everything has been cut out)
    the virtual B::f() is not overloaded, so instead of a virtual call it was a simple call, B::f() has no member addressing, so access by "pointer" was also cut out

    In the second case, A::f() is an attempted virtual call, there is access by "pointer", but no a1 object, because an empty copy constructor A::A(const A &) was also called for A *a1=b
 
Ilyas:

Please don't bank on this misguided behaviour.

There are 2 problems here(?):

I hope you will fix this erroneous behaviour? (make a compilation error) Because it is possible to make such an error by accident and the compiler does not react in any way.

 
Alexey Navoykov:

I hope you will fix this erroneous behaviour? Because it's possible to make such an error by accident and the compiler doesn't react in any way.

Yes, we will fix it.

The priority is set low because only empty classes get working code

 
Ilyas:

Please don't bank on this misguided behaviour.

There are 2 problems here(?):

  1. B *b1=a - an obvious error, the compiler perceives this construct as a call to the copy operator A::A(const A &)

  2. In the first case, the optimizer has determined that A::A(const A &) is empty, no members - no access by "pointer" (in fact, there is no code for calling the copy operator, everything has been cut out)
    the virtual B::f() is not overloaded, so instead of a virtual call it was a simple call, B::f() has no member addressing, so access by "pointer" was also cut out

    In the second case, A::f() is an attempted virtual call, there is access by "pointer", but no a1 object, because an empty copy constructor A::A(const A &) was also called for A *a1=b

Thanks for the detailed answer, however, I'm a bit confused about the logic.

1) Why does the compiler see the construct B* b1=a as a call of the copy operator A::A(const A&), (instead of calling B::B(const A&), because it is class B to the left of operator=)

2) Why didn't the compiler generate the warning "copy constructor missing"?

3) why a "simple" method call is allowed for non-existing objects (while an attempt to call B::f() directly gives a compilation error "not a static method call")

4) And why does the compiler allow a "simple" virtual method call at all? In my opinion, virtuality should not depend on presence or absence of data in the object

 
Ilya Malev:

1) Why does the compiler see B* b1=a as a call to copy operator A::A(const A&), (instead of calling B::B(const A&), because it is class B to the left of operator=)

That's exactly right. This is another MQL bug, that the compiler freely allows such things: B b = a; This is a violation of encapsulation principles. In C++ such things do not compile naturally, an explicit conversion is required. And in old MQL builds such things could not be done as well.

Reason: