c / c ++ - Basic Concepts

RAM

  • Management stack area and heap area is different: the stack area by the system memory allocation and release, without programmer control; heap memory is completely controlled by the programmer, think how much allocation to allocate how much, like when to release it when release, very flexible.
  • Stack (Stack) can store function arguments, local variables scope data, etc. within the local array function, its purpose is to complete the function call.
  • Memory stack area and heap area can be allocated during program execution based on actual demand and release, not just when the program started on an ample supply of all memory. This is known as dynamic memory allocation.
  • Created out on the stack object has a name, such as stu, using a pointer to it is not necessary. But by new objects created out is not the same, it is allocated on the heap memory, no name, only get a pointer to it, so you must use a pointer variable to receive the pointer, otherwise you can no longer find the object the more there is no way to use it. That is, using it to create a new object on the heap is anonymous, can not be used directly, you must use a pointer to it, and then with pointers to access its member variables or member functions.
  • Stack memory is managed automatically, you can not use delete to delete objects created on the stack; heap memory after the object is deleted after use can be managed by the programmer through delete. In the actual development, new and delete often in pairs, in order to ensure timely deleting objects no longer in use, to prevent the accumulation of useless memory.
  • With the object pointer, by the arrows ->to access object member variables and member functions, and by this structure pointer members similar to access it.
  • Member variable allocated heap memory area or stack area, the member function to allocate memory in the code area. Size of the object affected only by the member variables and member functions does not matter.

Pointer, reference

  • Is the process of passing on a nature assignment parameter assignment is to copy the memory. The so-called memory copy means copying a block of memory on the data to another piece of memory.
  • For basic data types like int, float, char, etc., they tend to occupy only a few bytes of memory, they are very fast memory copy. The array is a collection of data, the number of data is not limited, may be small, it may be tens of thousands, memory copy them there could be a lengthy process, will seriously slow down the efficiency of the program, in order to prevent skills poor programmers to write inefficient code, C language does not support direct assignment of the data set from the grammar.
  • In addition to the C language, C ++ , the Java , the Python  other language chunk of memory is prohibited to copy, in a similar manner using the bottom pointers are achieved.
  • C / C ++  prohibited contents of the array transfer function call directly, but forced transfer array pointer , and no such limitation on the structure and the object, may be passed when calling a function pointer, the content may be transmitted directly; in order to improve efficiency, We recommend passing pointers.
  • In C ++, we have a more convenient way than a pointer type data transfer polymerization, that is a reference (Reference) .
  • Reference data can be seen as an alias by the name of the alias and the original were able to get this data.
  • Parameter passing by reference use more intuitive than the form of the pointer. After the C ++ programming, I encourage readers to use a large number of references, it can generally be replaced by a pointer (of course also essential pointers in C ++), C ++ Standard Library is to do so.
  • In fact, just a reference pointer for a simple package, it is still the underlying achieved by a pointer, and pointers referenced memory length memory footprint occupied, is 4 bytes in the 32-bit environment, in the 64-bit environment 8 bytes, that he could not get the address cited, because the compiler conducted an internal conversion.
  • Although reference pointer-based implementation, it is easier to use than the pointer, can be seen from the two examples above it, and needs to get data through pointers *, writing trouble, need not be referenced, and it is common to use variable same. C ++ inventor Bjarne Stroustrup also said that the direct purpose he introduces references in C ++ is to make writing code more beautiful, especially in the operator overloading in, without the aid of a reference sometimes makes the use of operators is very troublesome.
  • The difference between pointers and references
    • Defining reference must be initialized, and later also faithfulness, can point to other transactions; the pointer is not restricted to this, when the pointer is not necessary to define the assignment after an arbitrary point data can be
    • You can have const pointer, but not const reference.
    • Pointer can be multiple levels, but there can be only one reference
    • Pointer and increment (++) quoted from minus (-) operator is not the same sense

Macro, inline functions

  • Macro definition is a "small thinking very dense" work, accidentally stepped on a pit, and not necessarily in the compilation and runtime discovery, to the program lay hidden.
  • In order to eliminate space-time function call overhead, C ++ provides a more efficient method, that is, at compile time a function call is replaced with the function of the body, similar to the C language in the macro expansion. This direct embedding at function call the function is called a function inline (Inline Function), also known as built-in functions or inline functions.
  • When writing C ++ code, I recommend using an inline macro function to replace parameters.
  • Inline function at compile time will be replaced with a function call at the function body, after the completion of the translation function does not exist, so the link will not lead to duplicate definition error. This and much like the macro, the macro is expanded during pre-treatment, there is no compile-time. From this perspective, inline functions more like macros during compilation.
  • Integrated content on this section and section, you can see an inline function has two main functions, one eliminating the overhead of a function call, the second is to replace the macro with arguments. But I prefer the latter, instead of macro arguments to better highlight the significance of the existence of an inline function.
  • When the function is more complex, space-time function call overhead is negligible, most of the CPU time will be spent on the implementation of the function body code, so we generally will be very short functions are declared inline.

Classes, objects

  • Class is just a template (Template), do not take up memory space is compiled, it is not the member variables are initialized in the definition of class, because there is no place to store data. Will only allocate memory for the member variable after creating an object, this time can be assigned up.
  • publicC ++ is adding a keyword, it can only be defined in a class, or member variables represent the class member functions have "open" access
  • Class can be understood as a new data type, the data type is the name of the Student. And char, int, float, etc. The difference is that the basic data types, Student is a complex data type, base type may comprise, but basically there are many types of properties not.
  • Created out on the stack object has a name, such as stu, using a pointer to it is not necessary. But by new objects created out is not the same, it is allocated on the heap memory, no name, only get a pointer to it, so you must use a pointer variable to receive the pointer, otherwise you can no longer find the object the more there is no way to use it. That is, using it to create a new object on the heap is anonymous, can not be used directly, you must use a pointer to it, and then with pointers to access its member variables or member functions.
  • Stack memory is managed automatically, you can not use delete to delete objects created on the stack; heap memory after the object is deleted after use can be managed by the programmer through delete. In the actual development, new and delete often in pairs, in order to ensure timely deleting objects no longer in use, to prevent the accumulation of useless memory.
  • With the object pointer, by the arrows ->to access object member variables and member functions, and a member to which it is accessed through a similar structure pointer.
  • This section highlights explains two ways to create objects: one is created on the stack, similar forms and definitions of common variables; the other is the heap using the new keyword to create, you must use a pointer to it, to the reader remember to delete the object out no longer used.
  • By using the object name to access member-point number ., use the arrow through the object pointer to access member ->, and this structure is very similar.
  • When the outer member functions defined in the class, it is necessary to add the class name to be defined in front of the function name. ::Resolution operator is called domain (also called scope operator or scope qualifier), for connecting the class name and function name to indicate the current function belongs to which class.
  • Member function must first be declared in the prototype class body, then to the definitions in the class, that class body should be in position before the function definition.
  • In vitro and class-based body member functions defined are distinguished: a member function defined in the class body is automatically inline functions , not defined in the class vitro. Of course, the function defined inside the class body can also add inline keyword, but this is superfluous, since internal body functions defined class default is inline function.
  • Inline function is generally not what we expected, it will replace the function call with the function of the body, so I suggest that within the class member functions body to make a statement, and is defined in an external class body, which is a good programming practice the actual development of everyone is to do so.
  • C ++ to control access to the member variables and member functions through public, protected, private three keywords, which represent the public, protected, private, known as a member access modifier. The so-called access is that you can not use members of the class.
  • In (outside the defined class code) outside the class, and can only be accessed by members of the public properties access the object through the object member, and can not access the private, members of the protected property.
  • Most of the member variable to m_begin with, it is vulgar agreed to the wording, not the syntax specified content. To m_begin with can see at a glance which is both a member variable, and can and member functions in the parameter name to distinguish.
  • According to C ++ software design specifications, member variables of the actual project development and member functions (only member function is called member functions) only within the class use are recommended declared as private, and only allow members function declaration by calling object is public. This member variables declared as private, the function declaration reflects some members of the class of public encapsulation approach. The so-called package, refers to an internal class that implements the hidden as much as possible, to provide a useful function only to the member user.
  • Member function to assign values to variables commonly referred to as set functions, their names are usually at setthe beginning, followed by the name of the member variables; function value of a member variable read function is often referred to get their names usually getbegin with, followed by the member variables first name.
  • In addition to functions set and get functions to create objects in Shihai can call the constructor to initialize each member variables, we will be in " C ++ constructors discussion" section. But only constructor to the member variable assignment once, later had to modify the aid set function.
  • Some readers may say, to add additional functions set and get functions much trouble, directly to members of the public and more variable to save! Indeed, in the case of 99.9% do so is not a mistake, I do not think there is anything wrong to do so; however, the private member variable to a software design specifications, especially in medium-sized projects, or Please try to follow this principle.
  • Declared as members of the private and statements for members of the public in any order, either comes first private part, you can first appear in the public section. If neither private nor write write public, it defaults to private.
  • Call the constructor is mandatory, once defined constructor in the class, then when you create an object had to be called, does not call wrong. If there are multiple overloaded constructor, then the argument must be provided when creating an object and a constructor which match; conversely, only a constructor is called when the object is created.
  • When you create an object on the stack, the argument behind object name, for example Student stu("小明", 15, 92.5f); create objects on the heap, it is located after the class name argument, for example new Student("李华", 16, 96).
  • Constructor must be public property, otherwise can not be called upon to create the object. Of course, set to private, protected property will not be an error, but does not make sense.
  • Constructor does not return value, since there is no variable receives the return value, if any useless. This means: either a declaration or definition, can not appear in front of the function name return type, even if the void is not allowed; function body can not have a return statement.
  • Use the constructor to initialize the list and there is no advantage in efficiency, it is merely convenient to write, especially when many member variables, such an approach is very simple and straightforward.
  • Order independent variable initialization sequence and initialization list of member variables listed, only the member variables in the order declared in the class concerned.
  • The only way to initialize a const member variables is to use initialization list.
  • Destructor (Destructor.) Is a special member function has no return value, the programmer need not explicitly call (not programmer to explicitly call), but is automatically performed when the object is destroyed. The name of the class constructor and the same name, but the name is the destructor in front of a class name added to ~the symbol.
  • C ++  is new and delete are used to allocate and free memory are the C language in malloc (), free () of the biggest difference is that: when using new memory allocation calls the constructor will during use delete release memory call the destructor. The constructor and destructor for the class is indispensable, so in C ++ are strongly encouraged to use the new and delete.
  • When you compile multiple files at the same time, if the first variable or function plus the static, it will be hidden from other source files. Using this feature you can define variables with the same name and function of the same name in different files, without worrying about naming conflicts.

Transformation, virtual functions, polymorphism

  • Class is actually a data type, data type conversion can occur, but this conversion only between the base and derived classes makes sense, and can only assign the derived class to the base class, including the derived class object to the assignment Object base class, the derived class pointer assigned to the base pointer, a derived class references cited assigned to the group, which is called up transition in C ++ (upcasting). Accordingly, the base class to the derived class assignment is called downward transition (Downcasting).
  • With virtual functions, base class pointer on the use of members of the base class (including the member variables and member functions) point to a base class object, use the pointing member of the derived class derived object. In other words, the base class may be a pointer to work according to the way the base class, or may be derived class way to do things, it has a variety of forms, or that manifested in many ways, we call this phenomenon polymorphism ( Polymorphism) .
  • C ++ provides polymorphic object: can be "full" refers to the access member variables and member functions for all derived classes (including direct and indirect derivative derived) by the base class, in particular member function. If there is no polymorphism, we can only access the member variables.

 

reference

Create objects on the stack or the heap

https://blog.csdn.net/weixin_33682790/article/details/92378612

Guess you like

Origin www.cnblogs.com/cxc1357/p/11781192.html