c++ - 在 C++ 中将指针数组分配给私有(private)变量

标签 c++ arrays pointers

在我的头文件中,我有以下代码

class ExEvent : public Event {
    public:
        ExEvent(
                Item* dst[SIZE],
                );
        ~ExEvent();
        Item* dst[SIZE];
};

在cpp文件中,我有如下代码

ExEvent::ExEvent(
        Item * dst[SIZE],
    ) : Event() {
    this->dst = &dst;
}

我收到以下错误:

error: array type 'Item *[15]' is not assignable
    this->dst = &dst;

有人能解释为什么会发生这个错误以及为什么我不能将 dst 数组指针分配给 this->dst。

最佳答案

在函数参数中 type[any-size] 实际上是 type*。 IE。 ExEvent(Item*[SIZE]) 实际上是 ExEvent(Item**)

因此,修复代码:

ExEvent::ExEvent(Item* src[SIZE])
{
    std::copy_n(src, SIZE, this->dst);
}

确保 src 有足够的元素。

参见 declaring functions: parameter list了解更多详情:

If the type is "array of T" or "array of unknown bound of T", it is replaced by the type "pointer to T"

关于c++ - 在 C++ 中将指针数组分配给私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40745367/

相关文章:

c++ - 通过事件和状态重用 boost msm 问题

arrays - 为什么 emgu 图像具有包含额外高度元素的数组?

java - 是否可以在接口(interface)中初始化数组?

c++ - 在值类型列表中保存引用类型?

c++ - 图像处理算法代码,解释指针

c++ - 在 operator new 和 "nested"初始化的情况下,实现应该做什么

C++ 文件访问/输入和输出

C++ 比较使用运算符的字符串

c - C 中的动态数组——我对 malloc 和 realloc 的理解正确吗?

c - 指针解引用线程安全吗?