GeekBand's first week study notes and geekband Study Notes

Source: Internet
Author: User

GeekBand's first week study notes and geekband Study Notes
Class member Modifiers (Class member modifier)

Public: can be accessed by any function or class.

Private: cannot be accessed by external functions and classes of non-friends;

Protected: can only be accessed by itself and its sub-classes;

Friend (youyuan ): Modify non-operation membersYou can specify an external function or class as an operating class's youyuan function (class). youyuan function (class) can directly access private members in the operation class. (PS: class objects <instances> mutual friends <youyuan>) Class with pointer member (s)

Class can be divided into Class with pointer member (s) and Class without pointer member (s) based on whether there are member pointers,For example, if a new memory is allocated during usage and no pointer points to the memory again after the class lifecycle ends, the memory needs to be released in the class destructor, otherwise, the program will be occupied until the end of the program, causing memory leakage.

(PS: pointer is the header address used by data in memory. Its size is related to the system's cable address range. In 32-bit systems, pointer occupies 32 bits, that is, 4 Byte, and 64 bits, that is, 8 Byte) Header file (Header file) defense statement

In Heard file, a macro definition must be declared in defense mode. OtherwiseThe header file is repeatedly included., Most compilers support

#ifndef NAME#define NAME......#endif

Some new compilers support # pragma once.

Inline function (inline function)

Function inline capabilitiesIncrease the program running speed but reduce the compilation speed and increase the total amount of code for the target program.It is suitable for functions with frequent calls and short code.

The declared and defined functions in the class structure take precedence over inline functions. For functions defined in the class structure in vitro, the inline keyword must be added before the declaration, whether the function is inline depends on the actual code size of the function and the compiler's judgment.

(PS: function inline replaces the function call jump with the actual function code by the compiler, which is different from macro definition) Ctor (constructor) and dtor (destructor)

  CtorAndDtorEach class will be called at the beginning and end of its lifecycle and will be called only once. By defaultCtorAndDtorExecute the memory allocation and release of class members respectively,CtorYou can input and reload parameters,DtorNo.

CtorThis parameter is usually set to public.CtorYou can use the public function in the class to obtain and limit the creation of instances.

(PS: dtor is usually used to release the member pointer pointing to the memory) Pass by reference & return by reference

Passing and returning parameters can increase the transmission speed. When passing any instance, it will only pass the first address of the instance and automatically unreference it.

(PS: this parameter cannot be used when it is of pointer type &) Operator overloading (operator overloading) operator overload declaration methods include:

    A: The overloaded operator is declared in the class body. It is declared in the same way as a common member function, except that its name contains keywords.OperatorAnd a pre-defined c ++ operator followed by it. class perason {bool operator> (type right );...}

B: Operator Overloading is implemented as a non-class member function (global function). For a global overload operator, parameters representing the left operand must be explicitly specified. bool operator> (type left, type right );

(PS: C ++ requires a value =, subscript [], call (), and the member pointing-> operator must be defined as a class member operator. any definition that defines these operators as namespace members will be marked as a compilation error .)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.