读书人

c++ templates 中 依赖性模板姓名疑问

发布时间: 2012-07-31 12:33:46 作者: rapoo

c++ templates 中 依赖性模板名称疑问
[code=C/C++][/code]
#include <iostream>

class C{
public:
static int size(){ return sizeof(C); }
private:
char t;
};

int C;

int size()
{
return sizeof(C);
}

int main()
{
std::cout << "C::size() " << C::size() << "\n";
std::cout << "::size() " << ::size() << std::endl;
system("pause");
return 0;
}

为什么 ::size()中的sizeof(C)不是类C而是整形变量C ??

[解决办法]
1. 代码最好不要这么写,这样只会引起混乱。
2. 如果你想::size()中的sizeof(C)不类C,那么就这么写吧:

C/C++ code
int size(){    return sizeof(class C);}
[解决办法]
探讨

首先很感谢你的回答
我是按照c++ templates 这本书中照样码下来的。
但是我有点不解,为什么编译器选择了整形变量C而不是类C,它参考的C++语言中那种机制?

读书人网 >C++

热点推荐