鑑於以下情況,工作代碼。不知道派生類型的靜態CRTP類?
#include <iostream>
template<class Detail>
class AbstractLogger
{
public:
static void log(const char* str) {
Detail::log_detailled(str);
}
};
class Logger : public AbstractLogger<Logger>
{
public:
static void log_detailled(const char* str) {
std::cerr << str << std::endl;
}
};
int main(void)
{
AbstractLogger<Logger>::log("main function running!");
return 0;
}
現在,我想把AbstractLogger
到庫中,並讓用戶庫定義自己的記錄,像Logger
類在這裏。這有一個缺點:AbstractLogger<Logger>
不能在庫中使用,因爲庫不能知道Logger
。
注:
- 請爲什麼不虛函數或問題。另外,我知道「靜態虛擬」成員是無效的類似問題。也許,在CRTP中有一個解決方法:)
- C++ 11會很有趣,但是,我需要「通常」的C++。
你是什麼意思*我想把'AbstractLogger'放入一個庫*。你的意思是你想在不知道實例化類型的情況下在庫中使用它嗎?或者你想要某種生成的代碼在庫中?要麼...? –
我的意思是第一個。 – Johannes
聞起來像一個沉重的設計缺陷和CRT模式的濫用... –