【发布时间】:2019-07-06 12:14:36
【问题描述】:
以下代码在我看来应该输出sum sizeof(int) + sizeof(float) + sizeof(std::string),但存储值始终为零。为什么?
struct Base {
static int IncrementID(int x) {
static int id = 0;
storage += x;
return id++;
}
static int storage;
};
int Base::storage = 0;
template<typename T>
struct Object : public Base {
static const int id;
};
template<typename T>
const int Object<T>::id(Base::IncrementID(sizeof(T)));
int main() {
Object<int> a;
Object<float> b;
Object<std::string> c;
std::cout << Base::storage;
}
【问题讨论】:
标签: c++