变量地址
我正在写统计系统。它应该使用给定的参数进行一些输出。例如:
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
}
}
看,每次更新调用我都需要所有传递变量的新值。所以我决定传递他们在内存中的地址。但现在数据由地址组成。我怎样才能从中获得价值?有可能吗?如果不可能,您对这个问题有什么建议吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存储在某个地址的数据由一元运算符*检索。
在您的代码中,您应该使用 *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.
我建议在这种情况下不要使用 BOOST_FOREACH,因为对于代码的读者来说,了解幕后发生的事情可能会有些困难。您可以使用普通迭代器重写更新函数:
但是,仅此并不能使您的代码正常工作。还有一些其他错误/糟糕的设计:
1)除非完全必要,否则不要使用 boost::any - 它是一个沉重的模板类,会减慢编译速度,并且不会提示内部存储的实际类型。更好的方法是创建类
EarthActivity
和SunActivity
,它们都派生自公共类Activity
,然后使用Activity* 而不是
boost::any*
作为地图参数。如果返回的值只是原始值(例如示例中的float
和int
),为什么 Sun 返回float
和 Earthint
?您应该对两者使用float
/double
。2) 您正在静态方法轨道内访问类
StatisticSystem
的成员,这是无效的并且无法编译。如果您尝试实现单例模式,有很多关于如何正确实现它的教程,例如这个:http://www.yolinux.com/TUTORIALS/C++Singleton.html3)您缺少函数的返回类型。如果您希望函数不返回任何内容,请指定
void
作为其返回类型: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:
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
andSunActivity
that would both derive from a common classActivity
and then useActivity*
instead ofboost::any*
as the map parameter. In case the returned values are only primitive ones (like thefloat
andint
in your example), why does Sun returnfloat
and Earthint
? You should usefloat
/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.html3) You are missing return types of the functions. If you want the functions to return nothing, specify
void
as their return type: