【问题标题】:C/C++: Pointers within Const StructC/C++:Const Struct 中的指针
【发布时间】:2011-03-11 17:30:50
【问题描述】:

如何在函数 fn 中强制 obj->val1 指向的内存为常量?

#include <iostream>

struct foo {
    int* val1;
    int* val2;
    int* val3;
};

void fn( const foo* obj )
{
    // I don't want to be able to change the integer that val1 points to
    //obj->val1 = new int[20]; // I can't change the pointer,
    *(obj->val1) = 20; // But I can change the memory it points to...
}

int main(int argc, char* argv[])
{
    // I need to be able to set foo and pass its value in as const into a function
    foo stoben;
    stoben.val1 = new int;
    *(stoben.val1) = 0;
    std::cout << *(stoben.val1) << std::endl; // Output is "0"
    fn( &stoben );
    std::cout << *(stoben.val1) << std::endl; // Output is "20"
    delete stoben.val1;
    return 0;
}

这里的代码很容易解释。我需要能够制作一个非常量对象并用数据填充它,然后将其传递给无法修改此数据的函数。我该怎么办?

我知道我可以只传入一个 const int 指针,但从理论上讲,这个类还包含其他几个我在“fn”中也需要的指针。

谢谢,

格里夫

【问题讨论】:

    标签: c++ c pointers struct constants


    【解决方案1】:

    由于您标记为 C++,您可以创建成员 private 并创建一个返回 const int * 的访问器。您最初可以通过构造函数或 friend 函数设置成员。

    【讨论】:

    • 啊。我忘记了 C++ 中的结构可以有方法和成员变量。这将解决我的问题,虽然只是为了咯咯笑,在 C 中是否有可能做到这一点?
    • @Griffin:如果你真的想确保你可能会单独传递它。
    • @Griffin:是的。请参阅我的答案,这对 C 和 C++ 都有效。
    • 唉,我发现自己无法接受这个答案 - 第一个 - 作为已接受的答案。当我勾选复选标记时,Stack Overflow 会显示一条错误消息。
    • @Brian R. Bondy:是的,我知道。我误读了这个问题;我删除了我的答案。
    【解决方案2】:

    我不是 C++ 人,但在 C 中,我会通过两种不同的结构声明来处理这个问题,一种是公共的,一种是私有的:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct private_foo {
        int* val1;
        int* val2;
        int* val3;
    };
    
    struct public_foo {
        int const * const val1;
        int const * const val2;
        int const * const val3;
    };
    
    
    void fn( struct public_foo * obj )
    {
        int local;
        *(obj->val1) = 20; // compile error
        obj->val1 = &local; // compile error
    }
    
    int main(int argc, char* argv[])
    {
        // I need to be able to set foo and pass its value in as const into a function
        struct private_foo stoben;
        stoben.val1 = malloc(sizeof(int));
        if (!stoben.val1) { return -1; }
        *(stoben.val1) = 0;
        printf("%d", *(stoben.val1) );
        fn( (struct public_foo *) &stoben );
        printf("%d", *(stoben.val1) );
        free(stoben.val1);
        return 0;
    }
    

    当我尝试使用 GCC 编译上述代码时,我收到以下编译器错误,因为我正在尝试修改只读内存:

    temp.c: In function ‘fn’:
    temp.c:20: error: assignment of read-only location
    temp.c:21: error: assignment of read-only member ‘val1’
    

    【讨论】:

    • 噢……现在这很聪明。向你致敬,先生。我正在将它添加到我的技巧包中。
    【解决方案3】:

    你真的不能。一个 const foo 指定里面的成员是 const,也就是说,它们是指向整数的常量指针,而不是指向常量整数的指针。

    对此的正确解决方案是通过封装、隐藏这些成员并提供公共接口。如果禁止修改 struct foo,一个实用的解决方案是通过私有继承:

    struct foo {
        int* val1;
        int* val2;
        int* val3;
    };
    
    struct constFoo : private foo {
    public:
       const int* getVal1() { return val1; }
       const int* getVal2() { return val2; }
       const int* getVal3() { return val3; }
    };
    

    当然,您需要创建适当的构造函数等,以便可以设置原始 foo。

    【讨论】:

    • 看来我得给我可怜的小结构增加一些额外的复杂性。感谢您提供信息丰富的代码示例!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多