#include <iostream>
using namespace std;
class A
{
public:
A();
~A();
int i = 0;//普通变量不能在类中进行初始化
/* error: ISO C++ forbids initialization of member ‘i’ */
static int j = 0;//只有静态常量整形才能在类中初始化,整形包括char short int long及其符号,不能为float、double或其他类型
/* error: ISO C++ forbids in-class initialization of non-const static member ‘j’ */
const int k = 0;//常量必须在构造函数的初始化列表中进行初始化
/* error: ISO C++ forbids initialization of member ‘k’; error: making ‘k’ static */
const static char *p = "Hello world"; //char *p 不是静态常量整形,所以不能在类中初始化
/* error: invalid in-class initialization of static data member of non-integral type ‘const char*’ */
static void fun();
};
A::A()
{
}
A::~A()
{
}
static void fun()
//静态函数只需在类中定义,静态函数定义时不能写static,而且需写成A::fun(),否则会当成一个全局函数
{
}
C++ 常见错误
最新推荐文章于 2022-08-04 15:14:48 发布