Passing an array of object pointers

 

Using the const specifier I can protect an array of objects from being resized:

class Foo
  {
  
  };

void resizeObjects(const Foo &arr[])
  {
   ArrayResize(arr, 1); // 'arr' - constant variable cannot be passed as reference
  }

Why doesn't this work for an array of pointers?

class Foo
  {
  
  };

void resizeObjects(const Foo* &arr[])
  {
   ArrayResize(arr, 1); // No problems (why?)
  }
 

Pointers have become const:

void resizeObjects(const Foo* &arr[])
  {
   const Foo* ptr1 = arr[0]; // Ok
         Foo* ptr2 = arr[0]; // '=' - cannot convert from const pointer to nonconst pointer
  }

But how can I protect the array from being resized?

 
Vladislav Boyko:

Using the const specifier I can protect an array of objects from being resized:

Why doesn't this work for an array of pointers?

Good question.

I would say it's a syntax issue. In first case all is const, the objects in the array and the array.

In the second case, the const apply to Foo only, it's an non-const array of pointers to const objects.

 
Vladislav Boyko #:

Pointers have become const:

But how can I protect the array from being resized?

No the pointers are not const, the objects are.

void resizeObjects(const Foo* &arr[])
  {
   ArrayResize(arr, 1); 
   arr[0] = new Foo;            // NOT const
   arr[0].i = 5;                        // 'i' - constant cannot be modified    StressTestEA.mq5        88      11
  }

You are declaring yourself a const OBJECT pointer then you try to assign it to a NON-CONST OBJECTpointer.

   const Foo* ptr1 = arr[0]; // Ok : this is defining a pointer to a const object, and NOT a const pointer to an object
 
Vladislav Boyko #:

But how can I protect the array from being resized?

Forgot to answer :

void resizeObjects(const Foo*const &arr[])
 
Alain Verleyen #:

Thank you very much for all the clarifications!

Alain Verleyen #:
No the pointers are not const, the objects are.

I've never thought about the difference between a const object and a const pointer before. Now I think I understand, here's my little research:

Pointer and object

// Const pointer to non-const object
Foo*const c_nc = new Foo();
//---
c_nc.i = 1;         // [Changing an object]:   OK
c_nc   = new Foo(); // [Changing the pointer]: 'c_nc' - constant cannot be modified
// Non-const pointer to const object
const Foo* nc_c = new Foo();
//---
nc_c.i = 1;         // [Changing an object]:   'i' - constant cannot be modified
nc_c   = new Foo(); // [Changing the pointer]: OK

Array of pointers and object (passing to a function)

// Non-const array of pointers to const objects
void test(const Foo* &arr[])
  {
   ArrayResize(arr, 1); // [Changing an array]:    OK
   arr[0] = new Foo();  // [Changing the pointer]: OK
   arr[0].i = 1;        // [Changing an object]:   'i' - constant cannot be modified
  }

void test2(Foo const* &arr[]) // (It's the same thing)
  {
   ArrayResize(arr, 1); // [Changing an array]:    OK
   arr[0] = new Foo();  // [Changing the pointer]: OK
   arr[0].i = 1;        // [Changing an object]:   'i' - constant cannot be modified
  }
// Const array of pointers to non-const objects
void test(Foo*const &arr[])
  {
   ArrayResize(arr, 1); // [Changing an array]:    'arr' - constant variable cannot be passed as reference
   arr[0] = new Foo();  // [Changing the pointer]: 'arr' - constant cannot be modified
   arr[0].i = 1;        // [Changing an object]:   OK
  }
// Const array of pointers to const objects
void test(const Foo*const &arr[])
  {
   ArrayResize(arr, 1); // [Changing an array]:    'arr' - constant variable cannot be passed as reference
   arr[0] = new Foo();  // [Changing the pointer]: 'arr' - constant cannot be modified
   arr[0].i = 1;        // [Changing an object]:   'i' - constant cannot be modified    test.mq5
  }
// Everything is non-const
void test(Foo* &arr[]);

I hope it's all combinations😄

 

The delete operator definitely does not change either an array or a pointer. This operator affects only the object.

Based on the code below, we can conclude that deleting an object does not change it.

class Foo
  {
   int i;
  };

// Non-const array of pointers to const objects
void test(const Foo* &arr[])
  {
   delete arr[0]; // OK
  }

You gave your friend your notebook to read and asked him not to write anything there. A friend burned your notebook and gave you back only the cover that contained the notebook😄

A friend did not write anything in a notebook - your request is not violated
 
Vladislav Boyko #:
class Foo   {    int i;   }; // Non-const array of pointers to const objects void test(const Foo* &arr[])   {    delete arr[0]; // OK   }
https://stackoverflow.com/questions/755196/deleting-a-pointer-to-const-t-const
Deleting a pointer to const (T const*)
Deleting a pointer to const (T const*)
  • 2009.04.16
  • Naveen Naveen 75.1k 47 47 gold badges 178 178 silver badges 234 234 bronze badges
  • stackoverflow.com
I have a basic question regarding the const pointers. I am not allowed to call any non-const member functions using a const pointer. However, I am allowed to do this on a const...
Reason: