c++ templates 中 依赖性模板姓名疑问
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,那么就这么写吧:
[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); }
------解决方案--------------------