Download MetaTrader 5

OOP : copy constructor and operator=() incorrect behavior

To add comments, please log in or register
eijiogata
36
eijiogata  

please suggest where wrong coding rules in this code.

when I written below class.

foo2, foo3 called unexpected copy constructor and operator=() each objects.

execution result paste in below.

class foo{
public:
   string s;
   foo(string a) { s = a; printf("foo constructor [%s]", a); }
   foo(const foo &sourceFoo) { printf("foo copy constructor source = [%s]", sourceFoo.s); } //copy constructor
   foo operator=(const foo &sourceFoo) { printf("foo operator= source = [%s], destination = [%s]", sourceFoo.s, s); s = sourceFoo.s; return this; }
};

void OnStart()
  {
   printf("foo1 create.");
   foo foo1("foo1");
  
   printf("foo2 create.");
   foo foo2 = foo1;  //instantiate by copy constructor
  
   printf("foo3 create.");
   foo foo3("foo3");
   foo3 = foo1;  //object deep copy by operator=()
  }

2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo copy constructor source = [foo1]
2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo operator= source = [foo1], destination = [foo3]
2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo constructor [foo3]
2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo3 create.
2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo copy constructor source = [foo1]
2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo operator= source = [foo1], destination = [(null)]
2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo2 create.
2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo constructor [foo1]
2014.05.07 02:25:27.421 OOPtest (AUDCAD,H1) foo1 create.

why foo2 initialize passed by operator=() ?

why foo3 initialize passed by copy constructor?

thanks.

Alain Verleyen
Moderator
31477
Alain Verleyen  

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


eijiogata
36
eijiogata  
angevoyageur:

many thanks to formed code. :-)

Alain Verleyen
Moderator
31477
Alain Verleyen  
eijiogata:

please suggest where wrong coding rules in this code.

...

why foo2 initialize passed by operator=() ?

Because you are using a copy, not ? Try this :

   printf("foo2 create.");
   foo foo2(foo1);  //instantiate by copy constructor

why foo3 initialize passed by copy constructor?

thanks.

Because "this" creates a copy.

   foo* operator=(const foo &sourceFoo)
     {
      printf("foo operator= source = [%s], destination = [%s]",sourceFoo.s,s);
      s=sourceFoo.s;
      return(GetPointer(this));
     }
Простак
315
Простак  
angevoyageur:

Because you are using a copy, not ? Try this :

Because "this" creates a copy.

It is the purest initialization here, NOT an assignment:

   printf("foo2 create.");
   foo foo2 = foo1;  //instantiate by copy constructor

Aren't you confused with the "(null)" value of s in the next line of the log?

2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo operator= source = [foo1], destination = [(null)]

The operator =() is called for an object that is NOT still constructed, and the next log line gives us a confirmation that the copy constructor for the object is called then, i.e. AFTER the operator =():

2014.05.07 02:25:28.476 OOPtest (AUDCAD,H1) foo copy constructor source = [foo1]

Obviously, and absolutely no doubts that this is the bug of one of the latest versions, because the code for the version 625 produces the log WITHOUT the wrong operator =() call:

2014.05.08 01:38:05.891 1 USDCHF,H1: foo copy constructor source = [foo1]
2014.05.08 01:38:05.891 1 USDCHF,H1: foo operator= source = [foo1], destination = [foo3]
2014.05.08 01:38:05.891 1 USDCHF,H1: foo constructor [foo3]
2014.05.08 01:38:05.891 1 USDCHF,H1: foo3 create.
2014.05.08 01:38:05.891 1 USDCHF,H1: foo copy constructor source = [foo1]
2014.05.08 01:38:05.891 1 USDCHF,H1: foo2 create.
2014.05.08 01:38:05.891 1 USDCHF,H1: foo constructor [foo1]
2014.05.08 01:38:05.891 1 USDCHF,H1: foo1 create.
2014.05.08 01:38:05.891 1 USDCHF,H1: initialized

As you can see, for the version 625 there is no operator =() call before the copy constructor call.

And "this" does NOT create a copy. Returning non-reference type from the operator =() creates a copy. But MQL4 compiler effectively resists use of references in return values in poor English: "'&' - reference cannot used". No doubts that MQL4 type scheme is designed defectively. For example, in C++ operator =() returns reference.

That's the truth.

Alain Verleyen
Moderator
31477
Alain Verleyen  
simpleton:

If you are thinking there is a bug, please report it to the ServiceDesk.

eijiogata
36
eijiogata  
angevoyageur:

If you are thinking there is a bug, please report it to the ServiceDesk.

sorry to delayed response.

angevoyageur:

very thanks to escorting thread. I've been very helpful.

I will report service desk for this issue.


simpleton:

I'm glad to teaching for me.

difficulty learn to MQL5 OOP class behavior...

eijiogata
36
eijiogata  

sorry to delayed response.

this issue was fixed at Build 945 on later.

many thanks to helps and advise. :-)

To add comments, please log in or register