【发布时间】:2010-08-01 02:32:58
【问题描述】:
有没有办法只调用一次函数?
假设我有一些课程
struct A {
void MainRoutine(Params) {
// Want to call other routine only once
}
void OtherRoutine(Params) {
// Do something that should be done only once and
// what depends on the params
}
};
我只想在MainRoutine 中调用OtherRoutine 一次(我假设MainRoutine 将被调用N 次。我不能从构造函数调用OtherRoutine,因为它接受@987654327 @ 在构造对象时可能不可用。
基本上我想做类似的事情
static bool called = false;
if (!called) {
OtherRoutine(Params);
called = true;
}
但我希望有一种更“漂亮”的方式来做到这一点……(可以写成一行)
也许使用boost::function 或boost 的某些部分我不知道? :)
谢谢
【问题讨论】:
-
一行?请改用
static const bool dummy = (OtherRoutine(params), true);。 ;) -
@Georg 好吧,我实际上是在想一些 OBVIOUS 行,但这也很酷)