C++ primer 笔记

来源:互联网 发布:中国m2历年数据曲线图 编辑:程序博客网 时间:2024/06/05 08:10
  • Class defines a type along with a collection of operations that are related to that type. A major design goal of C++ is to let programmers define their own types that are easy to use as the built –in types.
  • Type determines the size and the layout of the variable's memory;
  • short is usually too short ,long often has the same size as int , use double for floating-point
  • variables defined outside any function body are default initialized to zero. variables of build-in type define inside a function are uninitialized.
  • a declaration makes a name known to the program and specifies the type and name of a variable. a definition creates the associated entity.
  • it is usually a good idea to define an object near the point at which the object is first used.
  • a reference define an alternative name for an already existing object.
  • pointers are used for indirect access to other objects,pointers defined at block have undefined values if they are not initialized.If possible, define a pointer only after the object to which it should point has been defines. If not ,initialize the pointer to nullptr or zero.
  • because we can not change the value of a const, it must be initialized. the compiler usually replace uses of the variable with its corresponding value during compilation.
  • we indicate that the pointer is const by putting the const after the *. A pointer is itself const says nothing about whether we can use the pointer to change the underlying object.Whether we can change that object depends entirely on the type to which the pointer points.
  • top-level const: the pointer itself is a const.
  • low-level const : a pointer point to a const object.
  • it is a common mistake among new programmers to forget the semicolon at the end of a class definition.
  • when the preprocessor see a #include, it replaces the #include with the contents of the specified header.
  • programmers should be aware that they must add appropriate "#include" and "using" declarations to headers. and in most case "using" should not be in headers.
  • when we initialize a variable using =,we are asking the compiler tho copy initialize,when we omit the =,we use direct initialization.
  • using "getline" to read an entire line.
  • string::size_type is the library types in a machine-independent manner. It is an unsign type big enough to hold the size of any string. also we have vector<int>::size_type.....;
  • when we mix strings and string or character literals, at least one operand to each + operator must be of string type. string literals are not standard library strings. It is important to remember that these types differ when you use string literals and library strings. We can not add string literal.
  • C++ programs use the "cname" versions of headers and not the name.h version.
  • Processing every element-->using range-base for
    //for( auto c : container)
  • Because references are not objects, we can not have a vector of references.
  • Indeed, the most common way of using vectors is to define an initially vector to which elements are added as theirs values become know at run time. Defining a vector of a specific size can result in poorer performance.
  • if the vector hold objects of a type that we can not default initialize, then we must supply an initial element value.
  • subscript does not add elements into container and can result out of range error. A good way is to use range for.
  • when we use iterator ,modern c++ programmers use != as a matter of habit. They do so for the same reason that they use iterators rather than subscript.
  • If the object is const,then begin and end return a const_iterator;if the object is not const, they return iterator. It is usually best to use a const type(such as const_iterator) when we only need to read but not to write to an object. as do the begin and end members ,these members return iterators to the first and one past the last element in the container. However, regardless of whether the vector or string is const,they return a const_iterator.
  • We can not initialize an array as a copy of another array, nor is it legal to assign one array to another.
  • the compiler oridinarily converts the array to a pointer. In most expressions, when we use an object of array type, we are really using a pointer to the first element in that array. increment operator applied to pointer to move from one element to the next.
  • unlike subscripts for vector and string, the index of the built-in subscript operator is not an unsigned type.
  • Roughly speaking, when we use an object as an "rvalue",we use the object's value(its contents). When we use an object as an "lvaule", we use the object's identity( its location in memory)
  • prefix(++i) avoid unnecessary work,it increment the value and return the increased value. The postfix(i++) must store the increment value and original value for return.
  • because "sizeof" returns a constant expression, we can use the result of sizeof expression to specify the dimension of  an array.
  • for switch,it can be useful to define a default label even if there is no work for the default case.
  • it can be useful to have a local variable whose lifetime continues across calls to the function. we obtain such object by defining a local variable as static. Each "local static object" is initialized before the first time execution passes through the object's definition."local statics objects" are not destory when a function ends; they are destory when the program terminates.
  • In C++ programmers generally use reference parameters . reference parameters that are not changed inside a function should be references to const.
  • We can not copy an array and when we use an array as function parameters, it is usually converted to a pointer.
  • If control reaches the end of main and there is no return,then the compiler implicitly inserts return of 0. Value return from main is treated as a status indicaator. A zero return indicates success, mos other values indicate failure.
  • default arguments ordinarily shoud be specified with the function declaration in an appropriate header.
  • inline mechanism is mean to optimaize small,straight-line functions that are called frequently. And usually we define inline function in headers. member function defineed inside the class are automatically inline.
  • A "const" following the parameter list indicates that "this" is a pointer to const. Member functions that use const int this way are const member functions.
  • we should prefer in-class construtor initializer list:   Sales_data(const std::string &s) : bookNo(s) {}
  • the only difference between using class and struct to define a class is the default access level.struct for public and class for private.
  • define a type member usually appear at the beginning of the class.
  • a class can allow another class or function to access its nonpublic members by making that class or function a friend:
    class Screen{

               friend class Window_mgr;

         friend void MyClass::print();

       };


  • static member functions are not bound to any object, they do not have a this pointer.static member function my not be declared as const.Moreover, in general ,we may not initialize a static member inside the class. Instead, we must define and initialize each static data memeber outside the class body.


















































0 0
原创粉丝点击