OOP, templates and macros in mql5, subtleties and uses - page 8

 
Alexey Navoykov:
Are you referring to their standard library? )

No, I mean that in MQL you can't declare an abstract virtual method without implementation. In MQL, virtual methods of a base class must always have an implementation, which is fraught with the problems you mentioned.

 
Ilya Malev:

There are not many basic basic interfaces in C#

Actually there are a lot.

Ilya Malev:

It's not all bad, in my opinion. There are not so many basic main interfaces in C#, in my opinion (I'm not a C# specialist), that you can't reduce their methods to one basic superclass and then inherit what you need.

P.S. To implement something multiple through constructions like <<<<>>>> is a bit of a pain in the ass. Better to do functions through operators, e.g. a==b calls a.compareto( b ), a[comparer]==b calls comparer.compare(a,b) etc.

Imho, it would be a terrible mishmash.

+ Calling virtual methods is not free.
 
Vasiliy Sokolov:

No, I mean that in MQL you can't declare an abstract virtual method without implementation. In MQL, virtual methods of a base class must always have an implementation, which is fraught with the problems you mentioned.

I'm not sure why you can't declare it without implementation? Abstract class methods have been supported in MQL for years.

 
Vasiliy Sokolov:

1. In fact, there are a lot of them.

2. imho, it would be a terrible mishmash.

+ Call of virtual methods is not free.

1. I'll know.

2. Let's see what happens, if I manage to get what I'm doing now right, I'll post it on the forum).

Not for free, yes. Any universal OOP solution turns out to be expensive, but if your purpose is to easily and beautifully build simple Expert Advisors and indicators (without special features), then it is worth it, imho.

 
Alexey Navoykov:

I don't really understand why you can't declare without implementation? Abstract class methods have been supported in MQL for years.

Because an entry like this will cause a compilation error:

class A
 {
public:
  virtual int f1() = 0;
  virtual int f2() = 0;
 };
 
class B: public A
 {
public:
  virtual int f1(){ return 1; } 
 };
 
void OnStart()
 {
   B b;
 }


 
Ilya Malev:

Because an entry like this will cause a compile-time error:

And the person thought it's impossible to declare such a method in MQL at all, as far as I understood from his post.

 

Few people know (even fewer know and use), but pure-virtualfunctions can have a body

class A
 {
public:
  virtual int f1() = 0;
  virtual int f2() = 0 { return(0); }
 };

They also have to be overloaded in the descendant class

 
Ilyas:

Few people know (even fewer know and use), but purely virtualfunctions can have a body

They also have to be overloaded in the descendant class

So interfaces can still have their own method code? Can it be called somehow? )

Just came across this recently...

 
Hmm, interesting feature... I take it this is a default implementation, which can be called in descendants as A::f2(). i.e.:
class B : public A
{
  virtual int f2() { return A::f2(); }
};
p.s. I tried it now though... Even if A::f2() has no body, the compiler does not react to such a call. I.e. I have to catch an error later in the runtime. No way.
 
Alexey Navoykov:
Hmm, interesting feature... I guess it's a default method, just to be called in descendants of A::f2().

Tested it - you're right in general =)

Alexey Navoykov:
p.s. Although, I've tried it now... Even if A::f2() has no body, the compiler reacts nothing to such a call.
I reacted with "stack overflow" )

Apparently it's a cockroach after all...

class A
 {
public:
  virtual int f1() = 0 { return 1; }
  virtual int f2() = 0;
 };
 
class B: public A
 {
public:
  virtual int f1(){ return A::f1(); } 
  virtual int f2(){ return A::f2(); } 
 };
 
void OnStart()
 {
   A*a=new B;
   Print(a.f1());
   Print(a.f2());
 }