The annotation of <<C++ primer>> {藤原豆腐坊自家用},annotationprimer

來源:互聯網
上載者:User

The annotation of <<C++ primer>> {藤原豆腐坊自家用},annotationprimer

The annotation of <<C++ primer>> 

{藤原豆腐坊自家用}




給變數名一個初始值幾乎總是正確的. 但不要求必須這麼做


C++的主要設計目的之一就是允許程式員自訂類型,而這些類型和內建類型一樣便於使用.


什麼是對象?

一般而言, 對象是記憶體中具有類型的地區,說的具體一些, 計算左值運算式就會產生對象.


關於初始化

C++支援兩種初始化方式, 

複製初始化 copy initialization

直接初始化 direct-initialization

int ival(123); //direct-initialization

int ival = 123; //copy-initialization


初始化不是賦值! 

C++裡面非常強調賦值和初始化不是同一操作!!

初始化指建立變數並給它賦初始值, 而賦值則是擦除對象的當前值並用新值代替. 


關於左值右值的定義.

1. lvalue (pronounced "ell-value"): An expression that is an lvalue may appear as either the
left-hand or right-hand side of an assignment.


2. rvalue (pronounced "are-value"): An expression that is an rvalue may appear on the right-
but not left-hand side of an assignment.

關於變數初始化的規則:

Initialization of Variables of Built-in Type


       Whether a variable of built-in type is automatically initialized depends on where it is defined. Variables defined outside any function body are initialized to zero. Variables of built-in type defined inside the body of a function are uninitialized . Using an uninitialized variable for anything other than as the left-hand operand of an assignment is undefined.


Initialization of Variables of Class Type
 
         Each class may also define what happens if a variable of the type is defined but an initializer is not provided. A class does so by defining a special constructor, known as the default constructor . This constructor is called the default constructor because it is run "by default;" if there is no initializer, then this constructor is used. The default constructor is used regardless of where a variable is defined.



注意! extern聲明不是定義, 也不分配儲存空間.事實上,他只是說明變數定義在程式的其他地方, 程式中變數可以聲明多次, 但只能定義一次.


啥是範圍(scope)?

用來區分名字的不同意義的上下文稱作 範圍.

 (大牛們總能把一個很感性的概念很具體淺顯的表述出來, 啊 膜拜lippman)


一般局部範圍和全域範圍對於C程式員來說都是很熟悉的. 這裡需要強調一下的是語句範圍(statement scope).

for(int val = 1; val< 10; val++)

這裡val變數就是個語句範圍裡面的變數. 它定義在for語句的範圍中,只能在for語句中是使用, 而不能在main函數中使用.


關於const的一些問題會開一貼集中整理好我遇到過所有關於const的問題 :)




引用:

        引用就是對象的另外一個名字. 不能定義參考型別的引用, 但可以定義任何其他類型的引用. (不能二次引用)

int ival = 1024;int &refVal = ival; // ok: refVal refers to ivalint &refVal2; // error: a reference must be initializedint  &refVal3 = 10; // error: initializer must be an object

初始化是指明引用指向哪個對象的唯一方法.

const 引用是指向const 對象的引用.


       This behavior is easiest to understand when we look at what happens when we bind a reference to an object of a different type. If we write

double dval = 3.14;const int &ri = dval;


the compiler transforms this code into something like this:

int temp = dval;  // create temporary int from the doubleconst int &ri = temp;  // bind ri to that temporary 


同學, 請你講講typedef的作用 ?

  • To hide the implementation of a given type and emphasize instead the purpose for which the type is used
  • To streamline complex type definitions, making them easier to understand
  • To allow a single type to be used for more than one purpose while making the purpose clear each time the type is used



每句成員是常量. 不能改變枚舉成員的值.枚舉成員本身就是一個常量運算式, 所以也可以用於需要常量運算式的任何地方. 每個enum都定義一種唯一的類型. 枚舉類型的對象的初始化或賦值只能通過其枚舉成員或同一枚舉類型的其他對象來進行.



類的資料成員:

定義變數和定義資料成員存在非常重要的區別:

          一般不能把類成員的初始化作為其定義的一部分. 當定義資料成員時, 只能指定該資料成員的名字和類型. 類不是在類定義裡定義資料成員時初始化資料成員, 而是通過建構函式的特殊成員函數控制初始化.


訪問標號:

      訪問標號負責控制使用該類的代碼是否可以使用給定的成員. 類的成員函數可以使用類的任何成員, 而不管其存取層級. 訪問標號 public, private可以多次出現在類定義中, 給定的訪問標號應用到下一個訪問標號出現為止.


          用class和struct關鍵字定義類的唯一差別在於預設存取層級: 預設情況下, struct 成員為public, 而class的成員為private.






未完~ 待更新






                           二零一五年二月  攝於湖南

                           某小學公路旁的櫻花(O(∩_∩)O~應該是櫻花吧)~








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.