Why the following code is giving me an error
Test.cpp:23:10: error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive] return array;
#include <iostream>
#include <stdio.h>
#define MAX_ELEMENTS 5
class CBase
{
public:
CBase()
{
for(int i = 0; i < MAX_ELEMENTS; i++)
{
array[i] = 0;
}
}
~CBase()
{
// Nothing
}
int * GetArray() const
{
return array;
}
private:
int array[MAX_ELEMENTS];
};
int main ()
{
CBase b;
return 1;
}
EDIT: I understand that I should return a const int * but then I tried something below which works fine, request to explain the reason for allowing this and not allowing the above.
#include <iostream>
#include <stdio.h>
class CBase
{
public:
CBase():ptr(NULL)
{
}
~CBase()
{
delete ptr;
}
int * ptr;
public:
int * GetPtr() const
{
return ptr;
}
};
int main ()
{
CBase b;
return 1;
}
const int * GetArray() const {...}int const*and notint*const int * const, just like you don't have to return aconst doubleor something. The return value is copied out.