循环中指向int的C++指针?

标签 c++ arrays pointers for-loop

好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案....

我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下:

标题:

class Array {
private:
    // Private variables
    unsigned int mCapacity;
    unsigned int mLength;
    void **mData;

public:
    // Public constructor/destructor
    Array(unsigned int initialCapacity = 10);

    // Public methods
    void addObject(void *obj);
    void removeObject(void *obj);
    void *objectAtIndex(unsigned int index);
    void *operator[](unsigned int index);
    int indexOfObject(void *obj);
    unsigned int getSize();
    };
}

实现:

GG::Array::Array(unsigned int initialCapacity) : mCapacity(initialCapacity) {
    // Allocate a buffer that is the required size
    mData = new void*[initialCapacity];
    // Set the length to 0
    mLength = 0;
}

void GG::Array::addObject(void *obj) {
    // Check if there is space for the new object on the end of the array
    if (mLength == mCapacity) {
        // There is not enough space so create a large array
        unsigned int newCapacity = mCapacity + 10;
        void **newArray = new void*[newCapacity];
        mCapacity = newCapacity;
        // Copy over the data from the old array
        for (unsigned int i = 0; i < mLength; i++) {
            newArray[i] = mData[i];
        }
        // Delete the old array
        delete[] mData;
        // Set the new array as mData
        mData = newArray;
    }
    // Now insert the object at the end of the array
    mData[mLength] = obj;
    mLength++;
}

void GG::Array::removeObject(void *obj) {
    // Attempt to find the object in the array
    int index = this->indexOfObject(obj);
    if (index >= 0) {
        // Remove the object
        mData[index] = nullptr;
        // Move any object after it down in the array
        for (unsigned int i = index + 1; i < mLength; i++) {
            mData[i - 1] = mData[i];
        }
        // Decrement the length of the array
        mLength--;
    }
}

void *GG::Array::objectAtIndex(unsigned int index) {
    if (index < mLength) return mData[index];
    return nullptr;
}

void *GG::Array::operator[](unsigned int index) {
    return this->objectAtIndex(index);
}

int GG::Array::indexOfObject(void *obj) {
    // Iterate through the array and try to find the object
    for (int i = 0; i < mLength; i++) {
        if (mData[i] == obj) return i;
    }
    return -1;
}

unsigned int GG::Array::getSize() {
    return mLength;
}

我正在尝试创建一个指向整数的指针数组,其简化版本如下:

Array array = Array();
for (int i = 0; i < 2; i++) {
    int j = i + 1;
    array.addObject(&j);
}

现在的问题是在每次迭代中对 j 使用相同的指针。所以在循环之后:

array[0] == array[1] == array[2];

我确信这是预期的行为,但这并不是我想要发生的,我想要一个指向不同整数的不同指针的数组。如果有人能在这里指出我正确的方向,将不胜感激! :)(我显然误解了如何使用指针!)

附言感谢大家的回应。我接受了解决我遇到的问题的那个!

最佳答案

我猜你的意思是:

array[i] = &j;

在这种情况下,您将存储一个指向临时对象的指针。在每个循环中,j 都分配在堆栈的堆栈地址中,因此 &j 产生相同的值。即使您返回不同的地址,您的代码也会在您存储指向临时对象的指针时导致问题。

此外,为什么要使用 void* 数组。如果您实际上只想要 3 个唯一的整数,那么只需执行以下操作:

std::vector<int> array(3);

它更像 C++,并且消除了各种错误。

关于循环中指向int的C++指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21906640/

相关文章:

c++ - 此 UTF-8 实现是实现定义的还是定义明确的?

c++ - C++ 字符串文字的安全性和可靠性如何?

c - 无法将二维 float 组分配给指针到指针到浮点

pointers - golang 指针上的指针作为函数参数

c# - 如何修改所选选项卡中的控件 - C#

c - 结构体数组和指针算术

C++,为什么array=ptr合法?

c++ - 将对象地址与 valgrind 泄漏报告中的地址进行匹配

ruby - ruby 中的数组乘法谜题

PHP将const数组合并到新的const数组而不嵌套