可可西

VC6和VS2008中C++编译器差异

1. for中定义变量作用域不同

for (int i=0; i<100; i++){}
for (int i=100; i<200; i++){}

以上代码在VS2008中可编译通过;在VC6.0下会提示" 'i' : redefinition "的编译错误。

2. static const变量能否在头文件中初始化

class A
{
public:
	static const int m_MAXNUM = 10;
};

以上代码在VS2008中可编译通过;在VC6.0下会提示出两个令人费解的编译错误。

error C2258: illegal pure syntax, must be '= 0'

error C2252: 'm_MAXNUM' : pure specifier can only be specified for functions

将代码修改为以下即可通过编译

//a.h
class A
{
public:
	static const int m_MAXNUM;
};
// a.cpp
const int A::m_MAXNUM = 10;

 

这些差异是我们需要在移植代码时考虑的,紧记!

posted on 2011-11-18 14:21  可可西  阅读(1457)  评论(2编辑  收藏  举报

导航