如何通过引用传递类成员的固定大小数组?

发布于 2025-02-09 14:32:19 字数 592 浏览 2 评论 0原文

我有一个包含固定大小数组的类节点。我还有另一个创建一个实例mynode的类,并调用一个函数,将5个值分配给数组中的字段。我想通过引用传递数组,以便该函数修改实际数组而不是副本,但我不知道如何修改。

节点:

class Node
{
public:
    // Constructor, destructor, other members, etc
    uint8_t mArray[5];
}

工人:

class worker
{
    void doStuff(uint8_t (&arr)[5])
    {
        arr[0] = 12;
        arr[1] = 34;
        arr[2] = 56;
        arr[3] = 78;
        arr[4] = 90;
    }

    int main()
    {
        Node *myNode = new Node();
        doStuff(myNode->mArray);
        // myNode->mArray is not modified
    }
}

I have a class Node that contains a fixed-size array. I have another class that creates an instance myNode and calls a function to assign 5 values to the fields in the array. I want to pass the array by reference so the function modifies the actual array and not a copy, but I can't figure out how.

Node:

class Node
{
public:
    // Constructor, destructor, other members, etc
    uint8_t mArray[5];
}

Worker:

class worker
{
    void doStuff(uint8_t (&arr)[5])
    {
        arr[0] = 12;
        arr[1] = 34;
        arr[2] = 56;
        arr[3] = 78;
        arr[4] = 90;
    }

    int main()
    {
        Node *myNode = new Node();
        doStuff(myNode->mArray);
        // myNode->mArray is not modified
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九厘米的零° 2025-02-16 14:32:19

是的,阵列已修改。一个最小可重现的示例:

#include <cstdint>
#include <iostream>

class Node {
public:
    uint8_t mArray[5];
};

class worker {
    void doStuff(uint8_t (&arr)[5]) {
        arr[0] = 12;
        arr[1] = 34;
        arr[2] = 56;
        arr[3] = 78;
        arr[4] = 90;
    }

public:
    int main() {
        Node *myNode = new Node();
        doStuff(myNode->mArray);
        for(auto v : myNode->mArray) {
            std::cout << static_cast<unsigned>(v) << ' ';
            std::cout << '\n';
        }
        delete myNode;
        return 0;
    }
};

int main() {
    worker w;
    return w.main();
}

这将打印预期:

12 34 56 78 90 

如果您在功能中使用node&amp;会更容易:虽然:

#include <cstdint>
#include <iostream>

class Node {
public:
    uint8_t mArray[5];
};

class worker {
    void doStuff(Node& n) {
        n.mArray[0] = 12;
        n.mArray[1] = 34;
        n.mArray[2] = 56;
        n.mArray[3] = 78;
        n.mArray[4] = 90;
        // or just:
        // n = Node{12,34,56,78,90};
    }

public:
    int main() {
        Node *myNode = new Node();
        doStuff(*myNode);
        // ...
        delete myNode;
        return 0;
    }
};

int main() {
    worker w;
    return w.main();
}

Yes, the array is modified. A minimal reproducible example:

#include <cstdint>
#include <iostream>

class Node {
public:
    uint8_t mArray[5];
};

class worker {
    void doStuff(uint8_t (&arr)[5]) {
        arr[0] = 12;
        arr[1] = 34;
        arr[2] = 56;
        arr[3] = 78;
        arr[4] = 90;
    }

public:
    int main() {
        Node *myNode = new Node();
        doStuff(myNode->mArray);
        for(auto v : myNode->mArray) {
            std::cout << static_cast<unsigned>(v) << ' ';
            std::cout << '\n';
        }
        delete myNode;
        return 0;
    }
};

int main() {
    worker w;
    return w.main();
}

This prints the expected:

12 34 56 78 90 

It'd be easier if you took a Node& in the function though:

#include <cstdint>
#include <iostream>

class Node {
public:
    uint8_t mArray[5];
};

class worker {
    void doStuff(Node& n) {
        n.mArray[0] = 12;
        n.mArray[1] = 34;
        n.mArray[2] = 56;
        n.mArray[3] = 78;
        n.mArray[4] = 90;
        // or just:
        // n = Node{12,34,56,78,90};
    }

public:
    int main() {
        Node *myNode = new Node();
        doStuff(*myNode);
        // ...
        delete myNode;
        return 0;
    }
};

int main() {
    worker w;
    return w.main();
}
久随 2025-02-16 14:32:19

当通过参数中的值传递时,从不复制C风格数组。当作为普通参数传递时,它会变成指向第一个元素的指针。因此,您无需使用参考,只需使用普通的指针或数组参数即可。

class worker
{
    void doStuff(uint8_t arr[5]) // same as uint8_t *arr
    {
        arr[0] = 12;
        arr[1] = 34;
        arr[2] = 56;
        arr[3] = 78;
        arr[4] = 90;
    }

    int main()
    {
        Node *myNode = new Node();
        doStuff(myNode->mArray);
    }
}

A C-style array is never copied when passed by value in a parameter. When passed as an ordinary argument, it decays into a pointer to the first element. So, you don't need to use a reference, just use an ordinary pointer or array parameter.

class worker
{
    void doStuff(uint8_t arr[5]) // same as uint8_t *arr
    {
        arr[0] = 12;
        arr[1] = 34;
        arr[2] = 56;
        arr[3] = 78;
        arr[4] = 90;
    }

    int main()
    {
        Node *myNode = new Node();
        doStuff(myNode->mArray);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文