在课程之间传递地图 [英] Passing a map between classes

查看:137
本文介绍了在课程之间传递地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我先前提出的问题有关.我没有设法解决那里的问题,但是现在我只是想更好地了解代码以弄清楚如何处理该问题.

This is in relation to an earlier question I had. I haven't managed to solve the problem there but for now I'm just trying to get better acquainted with the code to figure out how to deal with that problem.

朝着这个目标迈进,我已经尝试了该问题中给出的建议,但对于以下内容为何行不通,我有些困惑.

Towards that goal, I've got around to trying out the suggestions given in that question and I'm a little stumped as to why the following isn't working.

我的标题中

class A {
public:
    typedef std::multimap<int, double> intdoublemap_t;
    const intdoublemap_t& getMap() const; 
    void setkey(int k);
    void setvalue(double v);
    void insertIntoMap();
    intdoublemap_t mMapA;

private:
    int key;
    double value;

};

class B {
public:
    typedef std::multimap<int, double> intdoublemap_t;
    void mapValues(const A& a);
private:
    intdoublemap_t mMapB;

};

在我的实现中

const A::intdoublemap_t& A::getMap() const { return mMapA; }
void A::setkey(int k) { key = k; }
void A::setvalue(double v) { value = v; }
void A::insertIntoMap(){mMapA.insert(std::make_pair(key, value));}

void B::mapValues(const A & a){ const A::intdoublemap_t& mref = a.getMap();
                                mMapB = mref; }

和main()

A a;
a.setkey(10);
a.setvalue(1232.2);
a.insertIntoMap();
B b;
b.mapValues(a);

代码可以正常编译,并且一切都与预期的工作有关,但映射完全没有传递给b.它保持空着

The code compiles fine and everything to do with a works as expected but the map is not passing to b at all. It stays empty

谁能告诉我为什么?

edit:我又看了一眼,看到了怎么做.我知道这是愚蠢的基本知识.我只需要在函数中将mref设置为B中的地图,然后可以调用函数以在B中的该地图上工作.

edit: I took another look at this and saw how to do it. I knew it was something stupidly basic. I just had to set mref in the function to a map in B and then could call a function to work on that map within B.

推荐答案

正如@FrancoisAndrieux所指出的,您的getMap()仅设置函数本地的引用,而不是类的intdoublemap_t mref.如果要让后者成为其他地方对地图的引用,则有三个选择:

As @FrancoisAndrieux notes, your getMap() only sets a reference local to the function - not the class' intdoublemap_t mref. If you want the latter to be a reference to a map elsewhere, you have three options:

  • 将其制成intdoublemap_t& mref,并在B实例的构造上对其进行初始化.
  • 将其制成std::reference_wrapper<intdoublemap_t> mref,并在需要时进行设置(例如在mapValues()中.
  • 将其设置为intdoublemap_t*(或在A和B中均为std::shared_ptr<intdoublemap_t>),并在需要时进行设置.
  • Make it intdoublemap_t& mref, initialize it on construction of the B instance.
  • Make it std::reference_wrapper<intdoublemap_t> mref, set it whenever you want (e.g. in mapValues().
  • Make it intdoublemap_t* (or std::shared_ptr<intdoublemap_t> in both A and B), set it whenever you like.

注意:正如@FrancoisAndrieux在评论中说的那样,使用第二个和第三个选项(并且不带std::shared_ptr),您将必须小心,不要在原始对象的生命周期到期后使用该引用. .

Note: As @FrancoisAndrieux says in a comment, with the second and third option (and without std::shared_ptr) you will have to be careful not to allow the reference to be used after the original object's lifetime has expired.

以上所述-我还必须说,您的设计对我而言似乎不太合适.您真的不应该执行任何这些操作,而且我99%的确定您以错误的方式来完成任务.

Having said all the above - I must also say that your design seems rather off to me. You should really not be doing any of these things and I'm 99% sure you're approaching your task the wrong way.

这篇关于在课程之间传递地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆