Download MetaTrader 5

OOP : copy constructor and operator=() incorrect behavior

To add comments, please log in or register
Share links from other resources. Now they have a description.
eijiogata
36
eijiogata 2014.05.06 19:32 

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
30755
Alain Verleyen 2014.05.06 20:02  

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 2014.05.06 20:31  
angevoyageur:

many thanks to formed code. :-)

Alain Verleyen
Moderator
30755
Alain Verleyen 2014.05.06 21:51  
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
Простак 2014.05.08 00:55  
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
30755
Alain Verleyen 2014.05.08 09:37  
simpleton:

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

eijiogata
36
eijiogata 2014.05.09 19:23  
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 2014.05.31 16:31  

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