Questions on OOP in MQL5 - page 81

 

Another question about casting

there is this code:

struct SHash
   {
      ulong hash[2];
      SHash()                                {  }
      SHash(const double)                    { ArrayInitialize(hash, 0xAAAAAAAAAAAAAAAA);                }
      SHash(const SHash &val)                { ArrayCopy(hash, val.hash);                                }
      bool operator==(const SHash &val)const { return(hash[0] == val.hash[0] && hash[1] == val.hash[1]); }
   };
//+------------------------------------------------------------------+
class BASE
{
private:
   const SHash _hash;
protected:
   BASE(SHash &hash): _hash(hash)             {  }
};

class A:BASE
{
public:
   A(SHash &hash):BASE(hash)                 {  }
};


class B:BASE
{
public:
   B():BASE((SHash)0.0)                      {  }
};

//+------------------------------------------------------------------+
void OnStart()
{
   SHash h(0.0);
   A a(h);
}
//+------------------------------------------------------------------+

and I want to initialize class B with constructor without parameters, but I can't cast SHash structure to constructor SHash(const double)

i.e. i need some casting (SHash) double

In my example, there is a compiler error: '(SHash)' - invalid cast operation


if you write it this way:

class B:BASE
{
public:
   B():BASE(0.0)                      {  }
};
error: '0.0' - parameter passed as reference, variable expected

 
Igor Makanu:

Another question about casting

there is this code:

and I want to initialize class B with constructor without parameters, but I can't cast SHash structure to constructor SHash(const double)

i.e. i need some casting (SHash) double

In my example, there is a compiler error: '(SHash)' - invalid cast operation


if you write it this way:

error: '0.0' - parameter passed as reference, variable expected

class B:BASE
{
public:
   B():BASE(SHash())                      {  }
};
 
Vladimir Simakov:

it works this way

but need 0xAAAAAAAAAAAAAAAA initialization for SHash structure - it's some kind of NULL - not getting MD5 hash from test string (i checked)

SHash() constructor seems to be not needed now, but seems to have been needed a few days ago ))) So, at first it wasn't there, then I fixed a compilation error somewhere with this empty constructor, or when comparing structures or during initialization... in general, there is a high probability that.... will be needed although it seems to be a good rule to initialize all variables....

in general, i wish i had a variant or unambiguous - no ))))

 
Igor Makanu:

it works this way

but need 0xAAAAAAAAAAAAAAAA initialization for SHash structure - it's some kind of NULL - not getting MD5 hash from test string (i checked)

SHash() constructor seems to be not needed now, but seems to have been needed a few days ago ))) So, at first it wasn't there, then I fixed a compilation error somewhere with this empty constructor, or when comparing structures or during initialization... in general, there is a high probability that.... will be needed although it seems to be a good rule to initialize all variables....

in general, i wish i had a variant or unambiguous - no )))

SHash(0.0)

but guess it by yourself?)))

 
Vladimir Simakov:

but guess it yourself?))

SHash((double)x)
 
Vladimir Simakov:

and guess it yourself?)))

class B:BASE
{
public:
   B():BASE(SHash(0.0))                      {  }
};

Oh, man, it works!

I think I tried it that way too, I don't know why it didn't work.

Thanks!

bad messed up ((!

 
Alexandr Andreev:

Yeah, and where do you get the x in the class constructor?))

 
Vladimir Simakov:

Yeah, and where do you get x from in the class constructor?)

the point is not about x) but that there may be a float on the receiving end, just 0.0 is not reliable either

 
Alexandr Andreev:

the point is not the x) but that there can be a float on the receiver besides the duble, and specifying just 0.0 is also unreliable

although all this is such a detail... not especially important

 
Alexandr Andreev:

the point is not about x) but that there can be a float in the input, just 0.0 is not reliable either.

It doesn't matter what to initialize there, even int - any number to call this constructor

and I chose 0.0 to avoid misprints - any digit, i.e. 0.0. is harder to write and misprint than 123

Reason: