变量地址

发布于 2024-09-16 14:30:21 字数 953 浏览 11 评论 0原文

我正在写统计系统。它应该使用给定的参数进行一些输出。例如:

float getSunActivity() { ... }
int getEarthActivity() { ... }

StatisticSystem::track("sun_activity", boost::any(getSunActivity()));
StatisticSystem::track("earth_activity", boost::any(getEarthActivity()));

class StatisticSystem
{
   typedef std::map<string, const boost::any*> stats;
   stats mStatsData;

   static void track(const string &name, const boost::any &param);
   void update();
};

StaticSystem::track(const string &name, const boost::any &param)
{
   mStatsData[name] = &param;
}

StaticSystem::update()
{
    BOOST_FOREACH(stats::value_type &row, mStatsData)
    {
        string data = lexical_cast<string>(&row.second);
        cout << data << "\n";
        // Usage of 'data' value
    }
}

看,每次更新调用我都需要所有传递变量的新值。所以我决定传递他们在内存中的地址。但现在数据由地址组成。我怎样才能从中获得价值?有可能吗?如果不可能,您对这个问题有什么建议吗?

I'm writing statistic system. It should make some output with given params. For example:

float getSunActivity() { ... }
int getEarthActivity() { ... }

StatisticSystem::track("sun_activity", boost::any(getSunActivity()));
StatisticSystem::track("earth_activity", boost::any(getEarthActivity()));

class StatisticSystem
{
   typedef std::map<string, const boost::any*> stats;
   stats mStatsData;

   static void track(const string &name, const boost::any ¶m);
   void update();
};

StaticSystem::track(const string &name, const boost::any ¶m)
{
   mStatsData[name] = ¶m;
}

StaticSystem::update()
{
    BOOST_FOREACH(stats::value_type &row, mStatsData)
    {
        string data = lexical_cast<string>(&row.second);
        cout << data << "\n";
        // Usage of 'data' value
    }
}

Look, each update calling I need in the new value of all passed variables. So I decided to pass their addresses in the memory. But now the data consist of address. How can I receive value from it? Is it possible, if not, what could you advice for this problem?

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

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

发布评论

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

评论(2

余生共白头 2024-09-23 14:30:21

存储在某个地址的数据由一元运算符*检索。

在您的代码中,您应该使用 *row.second 来获取值,而不是第二个的地址。

不过,所有这些代码看起来都很奇怪。在我看来,地址操纵技术是相当有问题的。

Data stored at an address is retrieved by unary operator*.

In your code, you should have used *row.second to get the value, rather than the address of second.

All this code looks peculiar though. The address manipulation technique is rather questionable, IMO.

唔猫 2024-09-23 14:30:21

我建议在这种情况下不要使用 BOOST_FOREACH,因为对于代码的读者来说,了解幕后发生的事情可能会有些困难。您可以使用普通迭代器重写更新函数:

void StaticSystem::update()
{
    for (stats::iterator it = mStatsData.begin(); it != mStatsData.end(); ++it)
    {
        string data = lexical_cast<string>(*it->second);
        cout << data << "\n";
        // Usage of 'data' value
    }
}

但是,仅此并不能使您的代码正常工作。还有一些其他错误/糟糕的设计:
1)除非完全必要,否则不要使用 boost::any - 它是一个沉重的模板类,会减慢编译速度,并且不会提示内部存储的实际类型。更好的方法是创建类 EarthActivitySunActivity,它们都派生自公共类 Activity,然后使用 Activity* 而不是 boost::any* 作为地图参数。如果返回的值只是原始值(例如示例中的 floatint),为什么 Sun 返回 float 和 Earth int?您应该对两者使用float/double

2) 您正在静态方法轨道内访问类StatisticSystem 的成员,这是无效的并且无法编译。如果您尝试实现单例模式,有很多关于如何正确实现它的教程,例如这个:http://www.yolinux.com/TUTORIALS/C++Singleton.html

3)您缺少函数的返回类型。如果您希望函数不返回任何内容,请指定 void 作为其返回类型:

void functionReturningNothing() { /* ... */ }

I suggest not using BOOST_FOREACH in this case as it might be somewhat harder for the reader of the code what's going on under the hood. You can rewrite the update function using plain iterators:

void StaticSystem::update()
{
    for (stats::iterator it = mStatsData.begin(); it != mStatsData.end(); ++it)
    {
        string data = lexical_cast<string>(*it->second);
        cout << data << "\n";
        // Usage of 'data' value
    }
}

However, this alone won't make your code work. There are some other bugs/bad design:
1) Do not use boost::any unless completely necessary - it's a heavy template class that slows down the compilation and gives no hint what actual types are stored inside. A better approach would be creating classes EarthActivity and SunActivity that would both derive from a common class Activity and then use Activity* instead of boost::any* as the map parameter. In case the returned values are only primitive ones (like the float and int in your example), why does Sun return float and Earth int? You should use float/double for both.

2) You are accessing a member of the class StatisticSystem inside the static method track, this is invalid and will not compile. If you are trying to implement a Singleton pattern, there are a lot of tutorials on how to make it correctly, for instance this one: http://www.yolinux.com/TUTORIALS/C++Singleton.html

3) You are missing return types of the functions. If you want the functions to return nothing, specify void as their return type:

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