日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

effective C++ 读书笔记(0-2)

發布時間:2024/4/17 c/c++ 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 effective C++ 读书笔记(0-2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1:?

?const int * ptr 指向常量(read only)的指針

? ? ? ?對指針重新賦值可以

? ? ? ?對指針指向的內容重新賦值不行

?int * const ptr 指向的指針常量

2:?

? ? ?在類的聲明中 聲明一個 static const 型成員變量 是可以的

? ? ?class A

{

private:

static const int num = 5;

int Array[num];

};

這里對于變量 num 不需要 在定義就可以使用

但注意這里 只能聲明 const型的成員變量

對于 static 非const 成員變量

class A

{

private:

static int num = 5;

int Array[num];

};

這樣子會報錯! ..\/basic.h:15:19: error: ISO C++ forbids in-class initialization of non-const static member 'num' ..\/basic.h:16:15: error: array bound is not an integer constant before ']' token

解決辦法 就是在.cpp文件中 定義咯~

4:

?the enum hack

?看起來好像 這和 static const unsigned ?成員變量 沒什么區別

class A

{

public:

static const int num = 5;

enum {numEnum = 5};

int scores[num];

int shit[numEnum];

};

? 但實際上 enum 的內容與define 很類似?

? 取一個 static const int 成員變量的值是合法的?

?但是取一個 enum 或是 define 出來的變量的值是不合法的

int main()

{

A a;

cout<<&a.num<<endl;

cout<<&a.numEnum<<endl; ?//error!

return 0;

}

enum分配出來的東西不會導致額外的內存分配 5: (1) 對于C-like 類型而言 (也即是 內置類型) pass-by-value 往往比 pass-by-reference 高效 (2)但是對于C++ 來講 由于存在 構造與析構函數 pass-by-reference-to-const 往往更高效 (3)對于template C++而言更是如此 應為我甚至不知道所處理對象的類型 所以pass-by-reference-to-const?比較好 #ifndef BASIC_H_ #define BASIC_H_ #include <iostream> using namespace std; class A { public: static const int num = 5; enum {numEnum = 5}; int scores[num]; int shit[numEnum]; template <class T> inline const T& callWithMax(const T& a,const T& b) { return (a>b?a:b); } }; class B { public : int a; inline bool operator >(const B &ref) const { return a>ref.a; } friend ostream& operator <<(ostream &os,const B &ref); B(int temp):a(temp){}; }; #endif /* BASIC_H_ */ #include "basic.h" ostream & operator <<(ostream & os,const B &b) { cout<<b.a<<endl; return os; } #include <iostream> #include <list> #include <map> #include "basic.h" using namespace std; int main() { A a; B b1(1),b2(2); cout<<a.callWithMax(b1,b2)<<endl; return 0; }

轉載于:https://www.cnblogs.com/wangshuai901/archive/2011/09/07/2169583.html

總結

以上是生活随笔為你收集整理的effective C++ 读书笔记(0-2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。