有沒有什麼辦法只能調用一次函數?C++ - 基本功能問題
假設我有一些類
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
}
};
我想打電話給OtherRoutine
僅在MainRoutine
(我假設MainRoutine
將被稱爲N
次,我不能從構造函數中調用OtherRoutine
一次,因爲它接受Params
,當物體被構造時可能不可用。
基本上我想要做類似於
static bool called = false;
if (!called) {
OtherRoutine(Params);
called = true;
}
但我希望有這樣做的更「漂亮」的方式...... (這可以寫在一行中)
使用boost::function
或boost
某些部分可能的東西,我不知道? :)
謝謝
單行嗎?試試'static const bool dummy =(OtherRoutine(params),true);'而不是。 ;) – 2010-08-01 02:39:30
@Georg嗯,我實際上正在考慮一些* OBVIOUS *行,但這也很酷) – 2010-08-01 02:41:01