Errors, bugs, questions - page 1184

 
A100:

Which pointer operations are not meaningless?

Anything for which references are normally used.
 
For example, an addition operator can be applied between references, which will call operator+(...). The addition of pointers makes no sense, because we will get a number that has no practical use.
 
A100:
What prevents operators that don't make sense from overloading them as objects or simply not implementing them?
 

This is the way it is now (overloaded as for objects) and everything seems to be fine. But there is a contradiction between the meaningful (== and !=) and the rest

class A {
public:
        bool operator ==( A* a ) { Print( __FUNCTION__ ); return ( true  );  }
        bool operator !=( A* a ) { Print( __FUNCTION__ ); return ( true  );  }
//остальные
        bool operator >>( A* a ) { Print( __FUNCTION__ ); return ( true  );  }
        bool operator +( A* a )  { Print( __FUNCTION__ ); return ( false  ); }
        bool operator !()        { Print( __FUNCTION__ ); return ( true   ); }
//... и т.д.
};

as

void OnStart()
{
        A *a = new A;
        if ( a == a ) Print( "1" ); //сравнение указателей как чисел
        if ( a >> a ) Print( "3" ); //вызывается operator<<( A *)
}

Which we suggest to eliminate by comparing pointers only through the special function

void OnStart()
{
        A *a = new A;
        if ( isEqualPoiner( a, a ) ) Print( "1" ); //сравнение указателей как чисел
        if ( a == a )               Print( "2" ); //вызывается operator==( A *)
        if ( a >> a )               Print( "3" ); //вызывается operator<<( A *)
}
 

A special function for comparing pointers can be designed as follows

template<typename T1, typename T2>
bool isEqualPointer( T1 *t1, T2 *t2 ) { return ( ulong(t1) == ulong(t2) ); }
 
A100:

I have an indiscreet question for you. Are you going to share your experiences?

You are one of the few people who actively and constantly use templates and macros. And you probably have a large amount of accumulated code.

I don't know about anyone else, but I would be very interested to see your code and your developments, if you have any, which you don't mind sharing with public.

I don't know... implementation of patterns, your wrappers for working with indicators, charts, whatever...

 

How do I use templates? - To shorten an entry. For example, there is no comma operation in MQL. Let's say there is a record

int f2( string str )
{
        Print( str );
        return ( 0 );
}

bool f( int a )
{
        if ( a == 5 )
                return ( true )
        f2( "abc" );
        return ( false );
}
Then having a pattern
template<typename T 1, typename T 2>
T2 opComma( T1 expression, T2 assignment_expression ) { return ( assignment_expression ); }

instead of 4 lines we have one

bool f( int a )
{
        return ( a == 5 ? true : opComma( f2( "abc" ), false ) );
}
 

How do I use macros? - Again - to shorten the record. For example, there are no class templates in MQL. Not a problem - we use a macro

#define  Macro( type ) \
class A {       \
public:         \
/*...еще много строк...*/ \
        type a; \
};

//осталось написать
Macro(  uint )
Macro( ulong )
 
Again to shorten the record - having once defined
#define  MDT( dt_struct, dt )     \
        MqlDateTime dt_struct;   \
        ZeroMemory( dt_struct ); \
        TimeToStruct( dt, dt_struct );

it will subsequently suffice to declare

datetime dt;
MDT( dt_struct, dt )
 

Bitwise copying, for example, is implemented through templates, since several types can be involved at once

template<typename T1, typename T2>
T2 BitwiseCopying( T1 type1, T2 )
{
struct Union1 {
        T1      type1;
};
struct Union2 {
        T2      type2;
};
        Union1 union;
        union.type1 = type1;
        return ( ((Union2)union).type2 );
}
void f()
{
        ulong ul = 0x16;
        double d = BitwiseCopying( ul, double(0)); //побитовое копирование из ulong в double
        float f = 36.6;
        uint ui = BitwiseCopying( f,    uint(0));  //побитовое копирование из float в uint

}
Reason: