Constructor Over Load Problem

 

Hi,

Im trying to use constructor over load but Ive faced a problem.

Ive created this class:

class test2
  {
protected:
   string            s;
public:
                     test2(string s);
                     test2(string s, int i);
   string            getS() {return s;};
                    ~test2();
  };
test2::test2(string s)
  {
   this.s=s;
   Print(this.s);
  }
test2::test2(string s,int i)
{
   test2(s);
   Print(this.s);
}
test2::~test2()
  {
  }

when I create an object of this class and run the constructor: test2("test",1);

test is only printed one time, and the second print is empty.

do you know why?

thank you.

 
yuv98:

Hi,

Im trying to use constructor over load but Ive faced a problem.

Ive created this class:

when I create an object of this class and run the constructor: test2("test",1);

test is only printed one time, and the second print is empty.

do you know why?

thank you.

Because doing that you create a temporary object when calling the first constructor. mql5 doesn't allow to call a constructor from an other constructor as you want.

You can either use a default parameter :

                     CTest(string s,int i=0) : CTest(s)
Or an private method which will be called from both constructors.
 

Ok,

Thanks mate.

Reason: