简体   繁体   English

课堂内外,为什么相同的数据有不同的值?

[英]Inside and outside the class, why the same data has different values?

As the topic, I was confusing this question for two night.作为话题,我把这个问题混淆了两个晚上。

I tried to use function build_BDDs to modify the value of map data structure outs, but the value printed inside the function is not the same as outside the function.我尝试使用函数 build_BDDs 来修改 map 数据结构 outs 的值,但是函数内部打印的值与函数外部不一样。

I changed the bool elements of struct map_Data to int type and had the same problem.我将 struct map_Data 的 bool 元素更改为 int 类型并遇到了同样的问题。 I suspect I'm using the wrong pointer, but can't figure out what's wrong.我怀疑我使用了错误的指针,但无法弄清楚出了什么问题。

here is my codes.这是我的代码。

#include <bits/stdc++.h>
using namespace std;

struct map_Data{
    bool hi;
    bool lo;
};
map<pair<int, int>, map_Data*> outs;

class MWVC{
    public:

    map_Data build_BDDs(map<pair<int, int>, map_Data*> & ins){
        map_Data txt;
        txt.hi = 1;
        txt.lo = 0;
        ins[make_pair(2,4)] = &txt;
        cout << ins[make_pair(2,4)]->hi << ";" << ins[make_pair(2,4)]->lo << endl;
 
        return * ins[make_pair(2,4)];
    }
};


int main(){
    MWVC ojb;
    map_Data a;
    a.hi = 1;
    a.lo = 0;
    outs[make_pair(1,2)] = &a;
    if(outs[make_pair(1,4)]){
        cout << outs[make_pair(1,4)]->hi << endl;
    }

    map_Data b = ojb.build_BDDs(outs);
   
    cout << outs[make_pair(2,4)]->hi << ";" << outs[make_pair(2,4)]->lo << ";" << b.hi << ";" << b.lo << endl;
    return 0;
}

and the result is结果是

1;0
0;0;1;0

Thanks advance.提前谢谢。

You build_BDDs function stores the address of a local variable in the map.您的build_BDDs函数将局部变量的地址存储在地图中。 When the function returns, the memory it points to can be overwritten at any time.当函数返回时,它指向的内存可以随时被覆盖。

For this instance, just drop the pointers and let the std::map worry about memory management:对于这种情况,只需删除指针并让 std::map 担心内存管理:

map<pair<int, int>, map_Data> outs;

and change build_BDDs to:并将 build_BDDs 更改为:

map_Data build_BDDs(map<pair<int, int>, map_Data> & ins){
        ins[make_pair(2,4)] = map_Data{1, 0};
        cout << ins[make_pair(2,4)].hi << ";" << ins[make_pair(2,4)].lo << endl;
 
        return ins[make_pair(2,4)];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么数组元素在循环内外具有不同的值? - Why the array elements are having different values inside and outside the loop? 为什么“this”指针在派生类和第一个基类中具有相同的值 - Why "this" pointer has same value inside derived and first base class 转换为具有相同数据成员布局但具有不同实现的类是否安全? - Is it safe to cast to a class that has the same data member layout, but a different implementation? 函数能否在循环内返回相同的值,而在循环外返回不同的值? - Can a function return the same value inside a loop, and return different values outside of loops? 类中相同变量的不同值 - Different values of same variable in class 使用在类外部声明的数据类型 - using a data type declared inside a class outside 为什么同一指针有不同的地址 - Why does same pointer has different addresses 为什么GPU外部的顶点转换与GPU内部的顶点转换不同? - Why vertex transformation outside GPU is not the same vertex transformation inside GPU? 为什么允许在类之外指定默认值但不在其中? - Why specifying the default value outside of a class is allowed but not inside it? 为什么数组大小初始化在类/ struct / ...内部或外部不同? - Why does array size initialization differ inside or outside a class/struct/…?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM