Errors, bugs, questions - page 1034

 
TheXpert:
As a matter of fact, it makes sense, because what is written looks a bit delusional.
C++ doesn't think so. And as for nonsense, this is as simplified an example as possible to get the point across.
 
A100:
C++ doesn't consider that way.

Yeah, C++ doesn't count a lot of things, but the point is that the initialisation of static members is probably not in the order in which they are initialised, but in the order in which they are declared.

I'll have to read the standard on this subject.

Документация по MQL5: Основы языка / Объектно-ориентированное программирование / Статические члены класса
Документация по MQL5: Основы языка / Объектно-ориентированное программирование / Статические члены класса
  • www.mql5.com
Основы языка / Объектно-ориентированное программирование / Статические члены класса - Документация по MQL5
 

I made it too easy - I didn't move const - as you can see now everything is clear - one constant is initialized by another one:

class A {
protected: //если заменить на public: то все работает
        static const int s1;
        static const int s2;
};

const int A::s1 = 0x1;
const int A::s2 = A::s1; //ошибка компиляции

The compiler indicates as an error that the s1 member is protected. If we replace protected with public, everything works like clockwork. But being protected has nothing to do with the initialization order or assignment order, nor with the possibility of initialization with a particular value

 
TheXpert:

I'll have to read the standard on this.

I agree, in general the compiler is not the same as the compiler and you have to sort it out.

Here is another example

enum _ENUM { en = 0x1 };
const int cc = 0x2;
static int s_array1[] = { cc }; //ошибка
static int s_array2[] = { en }; //нормально

class A {
public:
        static const int s;
        static int Array[];
};
const int A::s = 0x3;
int A::Array[] = {
        en,     //нормально
        A::s,   //ошибка
        cc      //ошибка
};

The MQL compiler here does not want to initialize the array with the const int value (enum - passes)

Again I'm appealing to the fact that C++ copies it and initializes correctly.

Документация по MQL5: Операции с массивами / ArrayInitialize
Документация по MQL5: Операции с массивами / ArrayInitialize
  • www.mql5.com
Операции с массивами / ArrayInitialize - Документация по MQL5
 
Probably an error in the call sequence
class A {
public:
        A *operator<<( int x ) { Print( x ); return ( GetPointer( this )); }
        A *operator<<( string s ) { Print( s ); return ( GetPointer( this )); }
};

int f( int& x ) { return ( x = 4 ); }

void OnStart()
{
        Print( "" );
        A a;
        int x = 3;
        a << "(1)" <<  "(2)" << "(3)"; // Результат: (1) (2) (3) //нормально
        a <<  x   << f( x ) <<  x;   // Результат:  4   4   3 ??? как же так?
}
In the last case I was expecting the result: 3 4 4 similar to the previous one. It is not normal for some types to have one call sequence and others to have a different one.
 
A100:
Probably an error in the call sequence
In C++, the order in which the operands of an expression are evaluated is UB. Why are you relying on a specific order here?
 
TheXpert:
In C++, the order of evaluating the operands of an expression is UB. Why are you relying on a specific order here?
Have you ever seen anywhere that
 cout << "Hello " << "word";

was output as "word Hello" ?

This is what happens in this case for an overloaded operator for the int type, while for the string type everything is output normally.

Moreover, it has nothing to do with C++. Suppose you wrote in your code

f(); // a << x;
g(); // a << f( x );
h(); // a << x;
And you have these functions executed in reverse order or at all for different types of arguments - in any order
 
By the way - the sequence of shift operations from left to right is also defined in MQL5 itself, as the example confirms:
void OnStart()
{
        int a = 0x1;
        int b = a << 2 << a;   //b = 8
        int c = (a << 2) << a; //c = 8
        int d = a << (2 << a); //d = 16
        Print ( "b=", b, ", c=", c, ", d=", d );
}
 

Greetings!

Help me fix 'invalid pointer access'.

In OnInit() we have:

SymbolsCollection = new CSymbolsCollection();
SymbolsCollection.RefreshRealSymbols();

CSymbolsCollection::RefreshRealSymbols() method has aCleanSymbolsArray(myRealSymbols) call;

These method and variable are declared in the class as:

public:
   CSymbol* myRealSymbols[];
   void CleanSymbolsArray(CSymbol* &inArray[]);

The problem is thatRefreshRealSymbols() executes normally if called in theCSymbolsCollection constructor.

However, calling SymbolsCollection.RefreshRealSymbols(); in OnInit(); results in:

invalid pointer access in 'SymbolsCollection.mqh' (55,22)

The error points to the CleanSymbolsArray(myRealSymbols); line in the body of the CSymbolsCollection::RefreshRealSymbols() method, the position is right after the opening bracket.

Any ideas?

 
vlad_123:
...

Any ideas?

Not this?