在下面的程序中,将打印“Here”:
在下面的程序中,“Here”没有被打印出来:
#include <iostream>
class Base
{
static bool temp;
static bool initTemp()
{std::cout<<"Here\n";return true;}
};
bool Base::temp = Base::initTemp();
class Derived : public Base
{};
int main() {int a;std::cin>>a;}
在下面的程序中,“Here”没有被打印出来:
#include <iostream>
template <class T>
class Base
{
static bool temp;
static bool initTemp()
{std::cout<<"Here\n";return true;}
};
template <class T>
bool Base<T>::temp = Base<T>::initTemp();
class Derived : public Base<int>
{};
int main() {int a;std::cin>>a;}
在两种情况下,Base都没有被引用。唯一的区别是在第二种情况下它是一个模板类。有人能解释一下为什么会出现这种行为吗?我正在使用VS 2012。
void main()
不是合法的 C++ 语法。应该使用int main()
。 - John Diblingtemplate bool Base<int>::temp;
- willj