Returning a pointer to an member array from const member function

[亡魂溺海] 提交于 2021-02-04 21:19:11

问题


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;
}

回答1:


Imagine code like this:

const CBase b;
int *array = b.GetArray();
array[0] = 5; // ooops! b changed but we declared it const!?

So as already mentioned in the comments, it does break const-correctness of your code. What you need to do is either declare GetArray() non-const, or make it return a pointer to a const int.

const int * GetArray() const 
{
    return array;
}

Now, code like this would not compile:

const CBase b;
const int *array = b.GetArray();
array[0] = 5;

EDIT To answer your other question from the comments above: When you call a method that returns a value and you assign this return value to some variable, than the return value is copied to your variable and afterwards discarded. So when your calling code changes the value of this variable this has no effect on the value or variable that was initially returned. That is why a const member function can return some of the class' data members. However, when you return a pointer to a data member, than the calling code can manipulate the value of this member. Still, the pointer is copied, but even the copy points to the memory location where the class member is stored and thus you could manipulate its value.




回答2:


Your method should return a const int* instead.

const int * GetArray() const
{
    return array;
}



回答3:


Its simple you need to either declare GetArray() non-const, or make it return a pointer to a const int.

const int * GetArray() const
{
    return array;
}

The reason is that if you return non constant array then as array is returned as pointer so its value can be changed by function which gets its value so constant function indirectly results in changing the value so you need to return constant array.



来源:https://stackoverflow.com/questions/21559731/returning-a-pointer-to-an-member-array-from-const-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!