码迷,mamicode.com
首页 > 编程语言 > 详细

c++基本语法整理

时间:2017-11-04 00:15:50      阅读:196      评论:0      收藏:0      [点我收藏+]

标签: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=Π              // Error, would allow *p=4 to change PI
  const double* p=Π        // OK, can‘t assign to *p (but may assign to p)
  double* const p=Π        // Error, may assign to *p (but not to p)
  const double* const p=Π  // 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 const
These 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 locations
The 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++基本语法整理

标签:location   cap   returns   before   and   operator   []   static   sig   

原文地址:http://www.cnblogs.com/enyala/p/7780468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!