摘要: 在C++98中,可以在头文件类的声明中使用“=”来给成员变量初始化,但是要求却很苛刻,被初始化的成员变量必须是:静态整形常量或者静态枚举类型。 1 class Init { 2 public: 3 Init():a(0){} 4 Init(int d):a(d){} 5 6 private: 7 int a; 8 const static int b = 0; 9 int c = 1; // 成员,无法通过编译10 static int d = 0; // 成员,无法通过编译11 static const double e ... 阅读全文
posted @ 2013-10-25 11:09 lniwn 阅读(499) 评论(0) 推荐(0) 编辑
摘要: 在C++98中sizeof运算符只能作用于类的静态成员,或者对象的非静态成员,所以对于类的非静态成员,必须先构造一个对象,这是相当麻烦的。 1 #include 2 using namespace std; 3 4 struct People { 5 int hand; 6 static People *all; 7 }; 8 9 int main() {10 People p;11 cout hand);但是在C++ 11中就可以直接使用如下方式:1 sizeof(People::hand) 阅读全文
posted @ 2013-10-25 10:05 lniwn 阅读(189) 评论(0) 推荐(0) 编辑
摘要: 在C++ 11中,声明一个类为另外一个类的友元时,不再需要使用class关键字,也可以使用typedef(或者using)定义的别名。 1 class Poly; 2 typedef Poly P; 3 4 class LiLei { 5 friend class Poly; // C++98通过, C++11通过 6 }; 7 8 class Jim { 9 friend Poly; // C++98不通过, C++11通过10 };11 12 class HanMeiMei {13 friend P; // C++98不通过, C++11通过1... 阅读全文
posted @ 2013-10-25 09:51 lniwn 阅读(245) 评论(0) 推荐(0) 编辑