Assignment Operator Overload (operator=) - page 3

 

This topic is really confusing at levels.

@RoboPogo : "any time a reference to an object is made, a function is called" That seems to me a really weird practice. What is a use case for this ?

    Object obj;
    Object *obj1,*obj2;
   
    obj1 = &obj;  // to be intercepted 
    obj2 = obj1;  // to be intercepted 

@Stanislav Korotky : "All you can do, if you want to intercept operations with a pointer, is to create another class, say SafePointer, which will control access to a contained pointer." So that means each time "a reference to an object is mode", you will have to do a duplicate statement to SafePointer object ? I am understanding well ?

  obj2 = &obj1;   // really usage
  obj4 = &obj1;   // duplicate call to SafePointer

@nicholishen : I can't see how your code is answering to the OP requirement ? There is no reference to an existing object in your code, a,b,c,d are 4 different objects. If you made "a reference to an object", nothing is intercepted.

   Object a(10),b;
   b=a;
   Object *c = new Object();
   c=b;
   Object *d = c.AssignNew();
   Object obj = d;
   Print("Object obj.val = ",obj.val);
   //this
   a=d;
   //is the same as this
   a.Assign(d);

If I am missing something, please enlighten me.

 
Alain Verleyen:

This topic is really confusing at levels.

@RoboPogo : "any time a reference to an object is made, a function is called" That seems to me a really weird practice. What is a use case for this ?

@Stanislav Korotky : "All you can do, if you want to intercept operations with a pointer, is to create another class, say SafePointer, which will control access to a contained pointer." So that means each time "a reference to an object is mode", you will have to do a duplicate statement to SafePointer object ? I am understanding well ?

@nicholishen : I can't see how your code is answering to the OP requirement ? There is no reference to an existing object in your code, a,b,c,d are 4 different objects. If you made "a reference to an object", nothing is intercepted.

If I am missing something, please enlighten me.

I don't have time to go through the entire thread, however, the OP was attempting to call his overloaded operator (a class method) on a null-pointer. Null-pointers, as you very well know, are incapable of performing any logic because they are, well, null. My code was to show OP alternatives to his conceived pattern. 

 
nicholishen:

I don't have time to go through the entire thread, however, the OP was attempting to call his overloaded operator (a class method) on a null-pointer. Null-pointers, as you very well know, are incapable of performing any logic because they are, well, null. My code was to show OP alternatives to his conceived pattern. 

No problem. Ok about the null-pointer issue, however, he was doing that while trying to "call a function any time a reference to an object is made", so when you have time and interest, it would be nice to confirm your code doesn't answer this question. Or to show how it answers it.

 
Alain Verleyen:

No problem. Ok about the null-pointer issue, however, he was doing that while trying to "call a function any time a reference to an object is made", so when you have time and interest, it would be nice to confirm your code doesn't answer this question. Or to show how it answers it.

There is no answer to OP - because OP didn't understand how pointers worked. There are only alternative options, which I attempted to share. 

 
Sorry if its not directly related to the post, but not long ago I recall seeing a new mql5 version with built in copy constructor called when assigning one object to another. Anyone can point me to this doc?


 
nicholishen:

Copy constructors are fairly straightforward, you can simply create an overloaded constructor that takes in the same class as an argument. 

You can step through this example to see what's happening.


No, I mean MQL5 now should support implicit copy of objects without coding the copy method. It should just copy objects if I recall correctly.
 

Ok, here it is at point 8. It is pitty that all changes are not consentrated in one place.

MQL5: Added automatic generation of an implicit copy operator for the objects of structures and classes. Now, the compiler automatically creates copy operators, which allows writing simple entries for objects, such as b=a:

class Foo
  {
   int               value;
public:
   string Description(void){return IntegerToString(value);};
   //--- default constructor
                     Foo(void){value=-1;};
   //--- parameterized constructor   
                     Foo(int v){value=v;};
  };
//+------------------------------------------------------------------+
//|  structure containing Foo type objects                           |
//+------------------------------------------------------------------+
struct MyStruct
  {
   string            s;
   Foo               foo;
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   MyStruct a,b;
   Foo an_foo(5);
   a.s="test";
   a.foo=an_foo;
   Print("a.s=",a.s," a.foo.Description()=",a.foo.Description());
   Print("b.s=",b.s," b.foo.Description()=",b.foo.Description());
//---
   Print("b=a");
   b=a;
//---
   Print("a.s=",a.s," a.foo.Description()=",a.foo.Description());
   Print("b.s=",b.s," b.foo.Description()=",b.foo.Description());
/*
   Execution result;
   a.s=test a.foo.Description()=5
   b.s= b.foo.Description()=-1
   b=a
   a.s=test a.foo.Description()=5
   b.s=test b.foo.Description()=5
*/
  }

Memberwise copying of objects is performed in the implicit operator.

  • If a member is an object, the corresponding copying operator for this object is called.
  • If a member is an array of objects, the receiving array is increased or reduced to the require size using ArrayResize before calling the appropriate copying operator for each element.
  • If a member is an array of simple types, the ArrayCopy function is used for copying.
  • If a member is a pointer to an object, the pointer is copied rather than the object to which it points.


If necessary, you can override the behavior and create your own option instead of an implicit copy operator, using overloading.

 
Amir Yacoby:
Sorry if its not directly related to the post, but not long ago I recall seeing a new mql5 version with built in copy constructor called when assigning one object to another. Anyone can point me to this doc?


Seems completely off-topic.
 
nicholishen:

Copy constructors are fairly straightforward, you can simply create an overloaded constructor that takes in the same class as an argument. 

You can step through this example to see what's happening.


The code you provided can be now redundant, no needing the copy constructor:

class Eggs
{
public:
   color col;
   int count;
   Eggs(){}
   // Eggs(Eggs &other):col(other.col),count(other.count){} - not needed
};


class Spam
{
public:
   Eggs eggs;
   Spam(){}
   // Spam(Spam &other):eggs(other.eggs){} - not needed
};


void OnStart()
{
   Spam spam1;
   spam1.eggs.col = clrBlack;
   spam1.eggs.count = 5;
   
   Spam spam2 = spam1;
   
   printf("spam2 has %d eggs with a color of %s", spam2.eggs.count, string(spam2.eggs.col));
}
 
Alain Verleyen:
Seems completely off-topic.

This is the relation, its not direct relation I agree.