首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-07-31 
c++ templates 中 依赖性模板名称疑问[codeC/C++][/code]#include iostreamclass C{public:static int

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++语言中那种机制?

热点排行