Talking about the PLO in the lounge - page 15

 
Vladimir Pastushak:

Where is it going all the way?


Check your mail.

 

Hello, moderators! I asked a normal question to which I received rudeness. If you tear it down, tear down operators and pointers nonsense too.

And don't let Sokolov appear in OOP topics at all, with such advocates of OOP on the forum he will die in agony.

 
Комбинатор:

Hello, moderators! I asked a normal question, to which I received rudeness, so if you tear it down, tear down the nonsense about operators and signposts too.

And don't let Sokolov appear in OOP topics at all, with such advocates of OOP the forum will die out in agony.

Have you clarified anything on your point of view? No.
I deleted a simple squabble.
 

A good example on the subject of OOP.

Graphics editor MT4
Graphics editor MT4
  • 2018.01.15
  • Evgeniy Serov
  • www.mql5.com
Утилита Graphics editor MT4 предназначена для создания и редактирования графических объектов типа: Удобный и интуитивно понятный интерфейс позволяет быстро и эффективно редактировать основные параметры графических объектов и сразу же видеть результат изменений. Входные параметры Languages - русский/английский; base_corner - угол привязки...
 
Artyom Trishkin:
Have you clarified anything on your point of view? No.
What was I supposed to clarify in a two-word question?
 
Vasiliy Sokolov:

Overriding operators gives nothing but syntactic sugar. It is better to abandon this practice and use Copy() or Clone() method instead of assignment operator - simple and clear:

class CMyClass
{
private:
   int m_state;
public:
   CMyClass* Copy()
   {
      CMyClass* obj = new CMyClass();
      obj.m_state = state;
      return obj;
   }
}


Vasily, thank you for your opinion. I agree with your approach with one small addition:

//+------------------------------------------------------------------+
//| Тестовый класс                                                   |
//+------------------------------------------------------------------+
class CMyClass
  {
private:
   int               m_state;

public:
   //--- конструктор\деструктор
   void CMyClass(){m_state=WRONG_VALUE;};
   void ~CMyClass(){};
   //--- копирование
   bool Copy(const CMyClass &_src_obj)
     {
      int source_state=_src_obj.m_state;
      if(source_state!=WRONG_VALUE)
        {
         this.m_state=source_state;
         return true;
        }
      return false;
     }
   //--- клонирование
   CMyClass *Clone(void)
     {
      CMyClass *obj=new CMyClass();
      if(CheckPointer(obj)==POINTER_DYNAMIC)
         obj.m_state=this.m_state;
      return obj;
     }
   //--- set\get
   void State(const int _src_state)
     {
      this.m_state=_src_state;
     }
   int State(void) const
     {
      return this.m_state;
     }
  };

Checking script:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- Объект 1
   CMyClass my_obj1;
   my_obj1.State(5);
   PrintFormat("Состояние Объекта 1: %d",my_obj1.State());
//--- Объект 2 - методом клонирования
   CMyClass *ptr_my_obj2;
   ptr_my_obj2=my_obj1.Clone();
   if(CheckPointer(ptr_my_obj2)==POINTER_DYNAMIC)
      PrintFormat("Состояние Объекта 2: %d",ptr_my_obj2.State());
//--- Объект 3 - методом копирования
   CMyClass my_obj3;
   my_obj3.State(3);
   PrintFormat("Состояние Объекта 3: %d",my_obj3.State());
   if(my_obj1.Copy(my_obj3))
      PrintFormat("Состояние Объекта 1: %d",my_obj1.State());
  }

Standard comparison characters are better left behind pointers.

What do you mean by pointers? In this sense?

CMyClass* ptr1,ptr2;
ptr2=ptr1;
 
Oh my God...
 
Комбинатор:
Oh, my...
Then what?
There is usually a justification and an option.
 
Artyom Trishkin:
Have you explained anything about your point of view? No.
I deleted a simple squabble.

I won't give in to provocations of the haters, who are also illiterate, but rather explain my point of view:

In normal programming languages(not C++),overloading the '=' operator is forbidden. And in some, operator overloading is forbidden almost completely, as it is quite deservedly considered anti-pattern there. I suggest that before using any such overloading, especially assignment operator, those who wish, think hard about why those stupid architects of those stupid languages do that.

Couldn't resist, personally to andrei: geez, don't embarrass yourself like that. You are saying such silly things: first about FP, now about operators. Want to hayterite - welcome: give references to authoritative sources, justify, etc.. What you are doing now is a rabid and most importantly totally illiterate hatred. You seem to be a programmer, like even a real one - it is already shameful to write such things.

 
Dennis Kirichenko:

Artem, you're wrong. What can you do without girls? - The girls are all ours :-))

But seriously, here is what I want to discuss. There is a class where the copy constructor is written and the assignment operator is overridden. Question. When should they be and must they be different?


The compiler itself creates a simple copy constructor that simply copies all the members of the class or structure.

If you want other actions to be performed while copying, you may define your own copy constructor

and assignment operator.

The assignment operator is called for an existing object, so it must first check whether the assignment

itself, then release the existing object if necessary, and then do the same thing as the constructor.