c++基本文法整理

來源:互聯網
上載者:User

標籤:location   cap   returns   before   and   operator   []   static   sig   

A pointer to a const object of type T must also be const, of type const T*, meaning that the pointer may be assigned to but its contents may not.  const double PI=3.1415926535898;  double* p=&PI;              // Error, would allow *p=4 to change PI  const double* p=&PI;        // OK, can‘t assign to *p (but may assign to p)  double* const p=&PI;        // Error, may assign to *p (but not to p)  const double* const p=&PI;  // OK, both *p and p are const/-----------------------------------A function name used without parenthesis(括弧) is a pointer to a function. Function pointers can be assigned values and called.  int f(double);     // functions f and g take double and return int  int g(double);  int *h(double);    // function h takes double and returns pointer to int  int (*p)(double);  // p is a pointer to a function that takes double and returns int  int main() {    p=f; p(3.0);     // calls f(3.0)    p=g; p(3.0);     // calls g(3.0)    p=h;             // Error, type mismatch/---------------------------------Explicit pointer conversions are allowed but usually unsafe.  int i, *p=&i;  i=int(3.0);        // OK, rounds 3.0  *(double*)p = 3.0; // Crash, writes beyond end of i  *(double*)&PI = 4; // Overwrites a constThese may also be written (with the same results):  i=static_cast<int>(3.0);            // Apply standard conversions  *reinterpret_cast<double*>p = 3.0;  // Pretend p has type double*  *const_cast<double*>&PI = 4;        // Same type except for const/----------------------------------The bare name of an array is a const pointer to the first element. If p is a pointer to an array element, then p+i points i elements ahead, to p[i]. By definition, p[i] is *(p+i).  int a[5];               // a[0] through a[4]  int* p=a+2;             // *p is a[2]  p[1];                   // a[3]  p-a;                    // 2  p>a;                    // true because p-a > 0  p-1 == a+1              // true, both are &a[1]  *a;                     // a[0] or p[-2]  a=p;                    // Error, a is const (but not *a)/--------------------------------------A literal string enclosed in double quotes is an unnamed static array of const char with an implied ‘\0‘ as the last element. It may be used either to initialize an array of char, or in an expression as a pointer to the first char. Special chars in literals may be escaped with a backslash as before. Literal strings are concatenated without a + operator (convenient to span lines).  char s[]="abc";                       // char s[4]={‘a‘,‘b‘,‘c‘,‘\0‘};  const char* p="a" "b\n";              // Points to the ‘a‘ in the 4 element array "ab\n\0"  const char* answers[2]={"no","yes"};  // Array of pointers to char  cout << answers[1];                   // prints yes (type const char*)  cout << answers[1][0];                // prints y (type const char)  "abc"[1]                              // ‘b‘Arrays do not support copying, assignment, or comparison.  int a[5], b[5]=a;       // Error: can‘t initialize b this way  b=a;                    // Error: can‘t assign arrays  b==a;                   // false, comparing pointers, not contents  "abc"=="abc"            // false, comparing pointers to 2 different locationsThe size of an array created with new[] may be an expression. The elements cannot be initialized with a list. There is no run time check against accessing deleted elements.  int n, *p;  cin >> n;  p=new int[n];  // Elements p[0] to p[n-1] with values initially undefined  delete[] p;    // Use delete with new or new(), delete[] with new[]  p[0] = 1;      // May crash/----------------------------------------static objects are placed in the data segment. They are initialized from values stored in the executable file, and therefore these values must be known at compile time. Initialization occurs only once. Values are maintained when the object is out of scope (e.g. between function calls), and it is safe to return a pointer or reference to them. Numeric values not explicitly initialized are set to 0.  int& f() {        // Return by reference, f() is an alias for s, not a temporary copy    static int s=1; // Initialized only once    ++s;    return s;       // Safe to return by reference  }  int main() {    cout << f();    // 2    cout << f();    // 3    f()=5;          // OK, s=5;    s=6;            // Error, s is not in scope

 

c++基本文法整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.