为什么我不能在带有 const 的函数内部使用此函数?

Why can't I use this function with inside a function with const?

本文关键字:函数 内部 const 不能 为什么      更新时间:2023-10-16

我在面向对象编程课程中使用无符号字符创建一个位数组。我得到了我们需要什么功能的基本布局。一个是我们的Query函数,它是const。

这是我的会员数据:

unsigned char* barray;         // pointer to the bit array
unsigned int arraySize;
static const unsigned int charSize = (sizeof(unsigned char) * 8);

这就是我遇到的问题:

bool BitArray::Query(unsigned int index)const{
unsigned int i = (Index(index)),
    p = Position(index);
unsigned int check = 1;
check = Move(check, p);
if ((check & barray[i]) == 0)
    return false;
else
    return true;

}

该函数以及运算符<lt;重载(也使用const关键字)当我在其中使用其他函数(Index、Position、Move)时会对我感到愤怒。

"IntelliSense:对象的类型限定符与成员函数"BitArray::Index"不兼容。对象类型为const BitArray"

发生了什么事?

/* determine which element in array of chars to use */
unsigned int BitArray::Index(unsigned int n){
unsigned int index = (n / charSize);
if (((n % charSize) == 0) && (index < 0)){
    index -= 1;
}
return index;

}

/* determine index of bit of the particular char */
unsigned int BitArray::Position(unsigned int n){
unsigned int position = n;
position -= ((n / charSize) * charSize);
if (position == 0)
    position = charSize - 1;
else
    position--;
return position;

}

unsigned int BitArray::Move(unsigned int n, unsigned int m){
    return n << m;

}

使用Microsoft Visual Studio Express 2013

其他函数也需要标记为const。如果它们不是const,那么就没有必要从const函数调用它们。