Some question about Pointer

 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CAbaco
  {
public:
   int               m_int;
                     CAbaco() {}
                     CAbaco(int num){m_int=num;}
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   CAbaco *one=new CAbaco(1);
   CAbaco two=new CAbaco(2);//Without empty constructor doesn't compile why?
                            //Call empty constructor and then the right one, why?

   delete one;
   delete GetPointer(two);//does not delete two object why?
  }
//+------------------------------------------------------------------+

Please help me answer questions in the code posted above:

1-with CAbaco object if  i don't declare empty constructor i get an error during compilation, why?

2- When compiling CAbaco object it call first the empty constructor and then the right one. Why? CAbaco* call the right one first try.

2- Why i get leak error if i try to delete CAbaco two object?

 
  1.  CAbaco *one=new CAbaco(1);
       delete one;
    1. Construct a newly allocated object with argument 1, getting its pointer.
    2. Assign the pointer to pointer one.
    3. delete the object using its pointer contained in one. No problems.

  2. Your code
    Equivalent with a copy constructor
    CAbaco two=new CAbaco(2);
       CAbaco* RHS = new CAbaco(2);
    CAbaco two(RHS);
      // RHS lost, leak
    Construct variable two (it's not a pointer, it's an CAbaco,) using the pointer. Calls copy constructor two.CAbaco(const CAbaco& RHS) if it existed, but it doesn't. The pointer RHS has now been lost. Leak error.

  3. Your code
    Equivalent with a assignment operator
    CAbaco two=new CAbaco(2);
    CAbaco two;
    {   CAbaco* RHS = new CAbaco(2);
        two = RHS;
    } // RHS lost, leak
    1. Construct variable two (it's not a pointer, it's an CAbaco,) using the the default constructor (which does exist.)
    2. Construct a new allocated object with argument 2, getting its pointer (RHS.)
    3. Assign the RHS object to variable two. Calls two.operator=(RHS) or compiler generated one.
    4. The pointer RHS has now been lost. Leak error.

  4.   delete GetPointer(two);//does not delete two object why?
    Variable two was not allocated dynamically, can't delete.
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CAbaco
  {
public:
   int               m_int;
                     CAbaco() {}
                     CAbaco(int num){m_int=num;}
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   CAbaco *one=new CAbaco(1);
   CAbaco *two=new CAbaco(2);//Without empty constructor doesn't compile why?
                            //Call empty constructor and then the right one, why?

   delete one;
   delete GetPointer(two);//does not delete two object why?
  }
//+------------------------------------------------------------------+
 
d.bignotti:

Please help me answer questions in the code posted above:

1-with CAbaco object if  i don't declare empty constructor i get an error during compilation, why?

   CAbaco two=new CAbaco(2);//Without empty constructor doesn't compile why?

As you have an explicit constructor, the compiler doesn't generate a default empty one. So it tries to call existing constructor and can't as you don't provide an int parameter.

Should be :

CAbaco two(2);  


And with default constructor (posted code) you have other problems, as well described by @whroeder1

See below article

The Order of Object Creation and Destruction in MQL5
The Order of Object Creation and Destruction in MQL5
  • 2010.03.01
  • MetaQuotes Software Corp.
  • www.mql5.com
not only opens up new possibilities for creating custom libraries, but also allows you to use complete and tested classes of other developers. In the Standard Library, that is included into the Initialization of global variables is done right after MQL5-program start and before any function call. During initialization initial values are...
 

I see the problem here. New operator create a pointer, i was trying to assign a pointer to a variable, to make that i need to declare a copyconstructor. Is that right?


So what's the advatage to use pointer over variable  of object? When i should use pointer and when is not recommend?

Another question:

int someFunc(CObject *obj) {}
int anotherFunc(CObject &obj) {}
//Same effect??

Passing object by & or * has the same effect?

 
d.bignotti:

I see the problem here. New operator create a pointer, i was trying to assign a pointer to a variable, to make that i need to declare a copyconstructor. Is that right?

So what's the advatage to use pointer over variable  of object? When i should use pointer and when is not recommend?

Passing object by & or * has the same effect?

  1. A copy constructor will not fix your lost pointer problem.
  2. I almost never use pointers, mostly an array of objects. MetaQuotes classes uses pointers to the cObject class, because of their broken template class.  Choice depends on your preferences.
  3. I always pass by reference, (and save a pointer if required.) They are equivalent except you can pass a null pointer, there is no such thing as a null reference. Only if the argument is optional, should you pass the pointer and test it internally..
 
whroeder1:
MetaQuotes classes uses pointers to the cObject class, because of their broken template class.

Incorrect. Stdlib collections make use of the polymorphic behavior inherited from the CObject virtual methods implemented by the subclass.

I see the problem here. New operator create a pointer

No, the new operator creates a new dynamically allocated object and assigns the address of this object to a pointer. A Pointer declaration is required at some point prior to creating a dynamic object. Look at Alain's example.

MyClass obj1;                         //static object
MyClass obj2(constructor_args);       //static object with parameterized constructor
MyClass *obj3;                        //pointer declaration
obj3 = new MyClass(constructor_args); //dynamic object allocation
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CNumber
  {
public:
   int               m_number;

                     CNumber() {}
                     CNumber(int number) {init(number);}
   void init(int number) {m_number=number;;}
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CMultiply
  {
public:
   CNumber           m_number;
   
                     CMultiply() {m_number.init(50); Print((m_number.m_number)*2);}
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   CMultiply number;
  }
//+------------------------------------------------------------------+

So in the above code, when i declare m_number static object, if i want to initialize it with a desired number i need to call a method, in this case init()? 

I can't declare as a member something like this, CNumber m_number(50); !!!!!

whroeder1:

2.I almost never use pointers, mostly an array of objects. MetaQuotes classes uses pointers to the cObject class, because of their broken template class.  Choice depends on your preferences.

Sometimes i need to sue pointer to operate on the same istance of t he object accross multiple other object, is that a good practice?
 
d.bignotti:

So in the above code, when i declare m_number static object, if i want to initialize it with a desired number i need to call a method, in this case init()? 

I can't declare as a member something like this, CNumber m_number(50); !!!!!

Sometimes i need to sue pointer to operate on the same istance of t he object accross multiple other object, is that a good practice?
Just like c++, you have to use the member initialization list in order to use the constructor on member class instances.
 
d.bignotti:

So in the above code, when i declare m_number static object, if i want to initialize it with a desired number i need to call a method, in this case init()? 

I can't declare as a member something like this, CNumber m_number(50); !!!!!

Sometimes i need to sue pointer to operate on the same istance of t he object accross multiple other object, is that a good practice?
class CNumber
  {
public:
   int               m_number;

                     CNumber() {}
                     CNumber(int number) {Set(number);}
   void              Set(int number) {m_number=number;;}
  };

class CMultiply
  {
public:
   CNumber           m_number;
   
                     CMultiply() : m_number(50) { Print((m_number.m_number)*2);}
                     CMultiply(int n) : m_number(n) { Print((m_number.m_number)*2);}
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   CMultiply number50;
   CMultiply anyNumber(18);
  }