Download MetaTrader 5
To add comments, please log in or register
Join MQL5 Cloud Network. Improve your rating!
xxx77
13
xxx77 2014.05.06 00:46 
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014"
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CData
  {
private:
   int               DataSize; // Number of elements in the Data[] array.
public:
   double            Data[]; // dynamic data storage
                     CData(void):DataSize(0){} // Default constructor, do nothing.
                     CData(const int size):DataSize(0){SetDataSize(size);} // Parametric constructor, call SetDataSize.
                    ~CData(){ArrayFree(Data);} // Default destructor.
   void              SetDataSize(const int size); // Sets the data size and allocates the necessary memory.
   int               GetDataSize() const {return(DataSize);}
  };
//+------------------------------------------------------------------+
//| Sets the DataSize with the size elements and allocates           |
//| the necessary memory for those elements                          |
//+------------------------------------------------------------------+
void CData::SetDataSize(const int size)
  {
   if(size>0)
     {
      DataSize=size;
      ArrayResize(Data,size);
     }
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

   CData *DataPointer; // Pointer to CData class.

   DataPointer=new CData(2); // Creates new CData class
   DataPointer.Data[0]=1; // Writes to DataPointer
   DataPointer.Data[1]=2;

   const CData*ConstData=GetPointer(DataPointer); // Pointer to constant CData
   CData*const ConstPointer=GetPointer(DataPointer); // Constant pointer to CData.

   ConstData.Data[0]=10; // Invalid, pointer to constant CData, writting not allowed.

   ConstPointer.Data[0]=10; // Valid, only pointer is constant.

   ConstData=new CData(2); // Should be valid, only data is constant, not the pointer!??!?!.

   ConstPointer=new CData(2); // Invalid pointer is a constant. 

  }
//+------------------------------------------------------------------+

Hello All,

I am having some troubles working in MQL4 with object pointers. The above code is an example to demonstrate the dificulties that I am having. In this code I declare 3 pointers to a class "CData" the first is a "normal" pointer called DataPointer, the second is a pointer to a constant CData object and finally the third is a constant pointer to a CData object.

The normal pointer DataPointer allows writting to its elements as expected:

DataPointer.Data[0]=1; // Writes to DataPointer
DataPointer.Data[1]=2;

The pointer to a constant CData object doesn't allow as expected, and doing so causes a compilation error:

ConstData.Data[0]=10; // Invalid, pointer to constant CData, writting not allowed.

The constant pointer to a CData object allows writting to its elements as expected because only the pointer itself is constant:

ConstPointer.Data[0]=10; // Valid, only pointer is constant.

Now the part that gets me confused is that the pointer to a constant CData object can not be made to point anywhere else as it should. The following line causes a compilation error :(

ConstData=new CData(2); // Should be valid, only data is constant, not the pointer!??!?!.

In any other object oriented programming language that I know this is allowed, is this a language bug? Or is it a feature of MQL4? I also searched the documentation and the forum to see if I am using that correct syntax and found nothing. Could anyone help me on this issue?

Thanks :)

Alexey Da
Moderator
6579
Alexey Da 2014.05.06 08:25  

We'll check it.

Thanks

Halley
198
Halley 2014.05.06 08:54  
Just a question - where could I find some specification about using const keyword syntax in MQL4 object declarations? I tried to search it, but nowhere it was mentioned. The documentation only describes using const in a function parameters list.
Alain Verleyen
Moderator
28627
Alain Verleyen 2014.05.06 10:59  
DeepThought:
Just a question - where could I find some specification about using const keyword syntax in MQL4 object declarations? I tried to search it, but nowhere it was mentioned. The documentation only describes using const in a function parameters list.
What do you want to know ?
Halley
198
Halley 2014.05.06 12:08  
angevoyageur:
What do you want to know ?
I hoped Alex could reply with a link.
whroeder1
13627
whroeder1 2014.05.06 13:51  
DeepThought: Just a question - where could I find some specification about using const keyword syntax in MQL4 object declarations? I tried to search it, but nowhere it was mentioned. The documentation only describes using const in a function parameters list.
   CData*const ConstPointer=GetPointer(DataPointer); // Constant pointer to CData.
  1. There isn't any except for: a const reference means you can only call const methods (type method(...) const{ ... },) and static members can be declared const
  2. There is nothing in the documentation about pointers to a const class.
  3. In fact a const pointer can't be passed as a non-constant reference and a pointer to const class can't be passed at all:
      void OnStart()
      {
       const testConst* tc1= new testConst();
       testConst* const tc2= new testConst();
       test2(tc1);  <<< 'tc1' - constant variable cannot be passed as reference
       test2(tc2);  <<< 'tc2' - constant variable cannot be passed as reference
      }
     void test2(testConst& tc){
      void OnStart()
      {
       const testConst* tc1= new testConst();
       testConst* const tc2= new testConst();
       test2(tc1); 
       test2(tc2);   <<< 'tc2' - constant variable cannot be passed as reference  }
     void test2(const testConst& tc){
    
    A constant pointer should be passable as a reference, and a pointer to a constant object should be passable as a constant reference. Build 625

xxx77
13
xxx77 2014.05.06 19:29  
alexvd:

We'll check it.

Thanks


Thanks :)
/
To add comments, please log in or register