Template function typification with objects and object pointers - page 2

 
Petr Nosek:

Your approach is clear to me. But I wanted to learn something new.

I've tried this:

but without success.

Try with MT5 (ME above 1601).
 
Alain Verleyen:
Try my MT5 (ME above 1601).
Your code is OK (and clear to me) but I try to compile nicholishen's code (without success)
 
Petr Nosek:
Your code is OK (and clear to me) but I try to compile nicholishen's code (without success)
Sorry I meant it doesn't compile with mql4, only mql5 (using new CFoo and not new T). 
 
Petr Nosek:

Your approach is clear to me. But I wanted to learn something new.

I've tried this:

but without success.

It won't compile because

  1. you need to use pointers when creating dynamic classes, and
  2. T has to be either  <CFoo*> or a CFoo ancestor. 

It's a pointless practice because this is a design pattern meant for polymorphism and not template classes... If you need a variance of classes based on a set of conditions then they need to inherit from a common ancestor. And even then you better be dynamically casting them at runtime and checking for errors...

 
nicholishen:

It won't compile because

  1. you need to use pointers when creating dynamic classes, and
  2. T has to be either  <CFoo*> or a CFoo ancestor. 

It's a pointless practice because this is a design pattern meant for polymorphism and not template classes... If you need a variance of classes based on a set of conditions then they need to inherit from a common ancestor. 

I know it is a pointless design pattern but I was curious if we can use pointer as an template <CFoo*>. I've never used it before and I'm not sure if it is possible. I've written two examples those should be the same. The first one without using templates and the second one with using templates. But the second one can't be compiled. My question is: Is it possible using pointers in templates?


The first example without templates:

class CFoo
{
public:
   void in(const string in) {A=in;}
   void out() {printf("A=%s",A);}
   string A;
};

CFoo* func(const string in)
  {
   CFoo* obj=new CFoo;
   obj.in(in);
   return(obj);
  }

void OnStart()
  {
   CFoo *foo;
   foo=func("foo");
   foo.out();
   delete foo;
  }

The second example without templates (can't be compiled):

class CFoo
{
public:
   void in(const string in) {A=in;}
   void out() {printf("A=%s",A);}
   string A;
};

template<typename T>
T func(const string in)
  {
   T obj=new CFoo;
   obj.in(in);
   return(obj);
  }

void OnStart()
  {
   CFoo *foo;
   foo=func<CFoo*>("foo");
   foo.out();
   delete foo;
  }
 
Petr Nosek:
I know it is a pointless design pattern but I was curious if we can use pointer as an template <CFoo*>. I've never used it before and I'm not sure if it is possible. I've written two examples those should be the same. The first one without using templates and the second one with using templates. But the second one can't be compiled. My question is: Is it possible using pointers in templates?


The first example without templates:

The second example without templates (can't be compiled):

@Petr  sure it's possible. As I said you already (not sure why you didn't get it ?) this code compiles fine with ME build 1755, but not with build 1601 (used with MT4 build 1090).
 
Alain Verleyen:
 this code compiles fine with ME build 1755, but not with build 1601 (used with MT4 build 1090).

I see


I tried to compile it with MT4. It is alright with MT5.

Conclusion:

  1. We can use pointers in templates in ME build 1755 and above (MT5)
  2. We can't use pointers in templates in ME build 1601 and below (MT4)

EDIT: The above conclusion is valid for using pointers as returned parameters of functions (like in my examples).

Thank you Alain for your patience.

Alain Verleyen:
 not sure why you didn't get it ?

Me neither. Maybe I'm too stupid.

 
Petr Nosek:

I see


I tried to compile it with MT4. It is alright with MT5.

Conclusion:

  1. We can use pointers in templates in ME build 1755 and above (MT5)
  2. We can't use pointers in templates in ME build 1601 and below (MT4)

Actually it's not exact. It's only when the pointer is used as return parameter of a function. So when you need to use something like :

func<CFoo*>

Example that compiles with ME 1601.

class CFoo
  {
public:
   void in(const string in) {A=in;}
   void out() {printf("A=%s",A);}
   string            A;
  };

template<typename T>
void func(const string in,T obj)
  {
   obj=new CFoo;
   obj.in(in);
  }

void OnStart()
  {
   CFoo *foo;
   func("foo",foo);
   foo.out();
   delete foo;
  }

Thank you Alain for your patience.

Me neither. Maybe I'm too stupid.

Certainly not. I was probably not clear enough.
 
Alain Verleyen:

Actually it's not exact. It's only when the pointer is used as return parameter of a function.

I wasn't clear enough. Of course I meant the case when the pointer is used as a returned parameter. I'll edit my previous post.

Reason: