Hundred battles c++ (1)

The difference between public and private

public and private are keywords in the class, which are used to specify the access rights of data or member functions in the class. Private type data or functions can only be accessed within the corresponding class, while public type data or functions have wider access permissions and can also be accessed in other classes or other functions.

Private member functions can be accessed in other classes by means of friend functions.

Can constructors be private?

Can.

A private constructor means that an instance can only be created within itself, and adding static can ensure that there is only one instance of the modified class.

This method is commonly used in singleton mode.

What is polymorphism and what is dynamic binding.

Polymorphism means that the same function can adopt multiple different behaviors according to different objects. Corresponding to it is static binding, that is, the function to be called is determined when the function is compiled. Dynamic binding, only when the program is running can the corresponding function be called according to the specific object.

There are three necessary conditions for the existence of polymorphism:
1. Inheritance;
2. Rewriting;
3. Parent class references point to subclass objects.

Callback function and callback mechanism

(1) Define a callback function;

(2) The party that provides the function implementation registers the function pointer of the callback function to the caller during initialization ;

(3) When a specific event or condition occurs, the caller uses the function pointer to call the callback function to process the event.

the function being called.

The role of the Static keyword

1. Global static variables

Add the keyword static before the global variable, and the global variable is defined as a global static variable.

Static storage area, which exists throughout the running of the program.

Initialization: Uninitialized global static variables are automatically initialized to 0 (the value of an automatic object is arbitrary unless it is explicitly initialized);

Scope: Global static variables are not visible outside the file in which they are declared, precisely from where they are defined to the end of the file.

2.   Local static variables

Add the keyword static before the local variable, and the local variable becomes a local static variable.

Location in memory: static storage area

Initialization: Uninitialized global static variables are automatically initialized to 0 (the value of an automatic object is arbitrary unless it is explicitly initialized);

Scope: The scope is still a local scope. When the function or statement block that defines it ends, the scope ends. But when the local static variable leaves the scope, it is not destroyed, but still resides in the memory, but we can no longer access it until the function is called again, and the value remains unchanged;

3. Static functions

Add static before the return type of the function, and the function is defined as a static function. The definition and declaration of a function are both extern by default, but a static function is only visible in the file where it is declared and cannot be used by other files.

The implementation of the function is modified with static, so this function can only be used in this cpp, and will not cause conflicts with functions of the same name in other cpp;

warning : Do not declare static global functions in the header file, and do not declare non-static global functions in cpp. If you want to reuse the function in multiple cpps, put its declaration in the header file, otherwise cpp Internal declarations need to be modified with static;

4. Static members of the class

In a class, static members can realize data sharing between multiple objects, and using static data members will not destroy the principle of hiding, that is, security is guaranteed. Therefore, a static member is a member shared among all objects of the class, not a member of an object. For multiple objects, only one static data member is stored and shared by all objects

5. Static functions of the class

Static member functions are the same as static data members, they all belong to the static members of the class, they are not object members. Therefore, references to static members do not need to use the object name.

In the implementation of a static member function, you cannot directly refer to the non-static members described in the class, but you can refer to the static members described in the class (this is very important). If a non-static member is to be referenced in a static member function, it can be referenced through an object. It can be seen from it that the following format is used to call a static member function: <class name>::<static member function name>(<parameter list>);

 Talk about the role of the static keyword in C++

For variable declarations outside of function definitions and code blocks, static modifies the link attribute of the identifier from the default external to internal, and the scope and storage type remain unchanged. These symbols can only be accessed in the source file where they are declared.

For the variable declaration inside the code block, static modifies the storage type of the identifier, changing from automatic variable to static variable, and the scope and link attributes remain unchanged. This variable is created before program execution and exists throughout the program execution cycle.

For ordinary functions modified by static, it can only be used in the source file that defines it, and cannot be referenced in other source files

For class member variables and member functions modified by static, they belong to the class, not an object, and all objects share a static member. Static members are accessed via <class name>::<static member>.

 Talk about the four cast conversions in c++

The four types of conversions in C++ are: static_cast, dynamic_cast, const_cast, reinterpret_cast

    static_cast<double>(a) / static_cast<double>(b);

1、const_cast

Used to convert const variables to non-const

2、static_cast

  (1 ) For the conversion of pointers or references between the base class and the derived class in the class hierarchy, it is
      safe to perform an up conversion (convert the pointer or reference of the derived class to the base class representation) and
      perform a down conversion (convert the pointer of the base class Or reference conversion to derived class representation), because there is no dynamic type checking, it is unsafe
    (2) Used for conversion between basic data types, such as converting int to char. The safety of this conversion also needs to be guaranteed by the developer
    (3) convert the null pointer to a null pointer of the target type
    (4) convert any type of expression to void type

3、dynamic_cast

Used for dynamic type conversion. It can only be used for classes with virtual functions , and is used for up and down conversion between class hierarchies. Only pointers or references can be transferred. When downcasting, if it is illegal, it returns NULL for pointers and throws an exception for references. It is necessary to understand the principle of internal conversion in depth.

Upcast: Refers to the conversion from a subclass to a base class

Downcast: Refers to the conversion from a base class to a subclass

It judges whether the downcast can be performed by judging whether the runtime type of the variable is the same as the type to be converted when the statement is executed.

4、reinterpret_cast

Almost anything can be converted, such as converting an int to a pointer, which may cause problems, so use it as little as possible;

5. Why not use C's mandatory conversion?

On the surface, C 's forced conversion seems to be powerful and can convert anything, but the conversion is not clear enough to perform error checking and is prone to errors.

Please tell me the difference between pointers and references in C/C++?

1. The pointer has its own space, and the reference is just an alias;

2. Use sizeof to see that the size of a pointer is 4, while the reference is the size of the referenced object;

3. The pointer can be initialized to NULL, while the reference must be initialized and must be a reference to an existing object;

4. When passed as a parameter, the pointer needs to be dereferenced to operate on the object, and direct modification of the reference will change the object pointed to by the reference;

5. There can be const pointers, but no const references;

6. The pointer can point to other objects during use, but the reference can only be a reference to an object and cannot be changed;

7. Pointers can have multiple levels of pointers (**p), ​​while references are at one level;

8. Pointers and references have different meanings using the ++ operator;

9. If you return an object or memory allocated by dynamic memory, you must use a pointer, and the reference may cause a memory leak.

1. Quote:

C++ is the inheritance of C language. It can carry out procedural programming, object-based programming characterized by abstract data types, and object-oriented programming characterized by inheritance and polymorphism. Reference is an important extension of C++ to C language. A reference is an alias of a variable, and the operation on the reference is exactly the same as the direct operation on the variable. Referenced declaration method: type identifier & reference name = target variable name; reference introduces a synonym for the object. The expression method of defining a reference is similar to that of defining a pointer , except that & is used instead of *.

2. Pointer:

Pointers use addresses, whose value points directly to a value stored elsewhere in the computer's memory. Since the required variable unit can be found through the address, it can be said that the address points to the variable unit. Therefore, what visualizes the address is called a "pointer". It means that the memory unit with its address can be found through it.

virtual function table

A class has only one virtual function table stored in the read-only data segment. The instantiated object of each class has a virtual function table pointer pointing to the virtual function table.

As long as the parent class has a virtual function table, no matter whether the subclass has a virtual function or not, there is a virtual function table. The virtual function table of the base class and the virtual function table of the subclass are not the same table

In single inheritance, the parent class has a virtual function table, and the subclass has a virtual function table. If the subclass rewrites the parent class virtual function, the corresponding content in the subclass virtual function table will be overwritten and point to the subclass function.

In multiple single inheritance, the subclass is modified on the basis of the parent class.

Multi-base inheritance: as many base classes as there are virtual function tables

1. Subclass virtual functions will overwrite every virtual function with the same name in every parent class.

2. The virtual function that is not in the parent class but exists in the subclass, fill in the first virtual function table, and the parent class pointer cannot be called.

3. If there are virtual functions in the parent class but not in the subclass, they will not be overwritten. Only the child class and the parent class pointer can be called.

4. If the subclass has a new virtual function, add it to the end of the first virtual function table.

The role of Explicit is

C++ provides the keyword explicit , which prevents implicit conversions that should not be allowed through conversion constructors . Constructors declared explicit cannot be used in implicit conversions.

Const keyword

int i = 0;

const int* p1 = &i; The value pointed to cannot be changed, but the pointer can be changed

int const* p2 = &i; The value pointed to cannot be changed, but the pointer can be changed

int* const p3 = &i; the pointer cannot be changed, the value can be changed

const int* const p4 = &i; cannot be changed

When  const a constant is passed in as a parameter, the constant must be a reference type   

void setName(const string& name); Only by passing by reference can it really play the role of const

void setName(const string name);  The value passed in is a copy and will not cause changes

Const objects can only access const member functions, non- const objects can access any member functions, including const member functions; members of
const objects cannot be modified, and objects maintained through pointers can indeed be modified;
const member functions cannot Modify the data of the object, regardless of whether the object is const or not . It is checked at compile time based on whether member data is modified.

Any function that does not modify data members should be declared const . If you accidentally modify the data member or call other non- const member functions when writing a const member function , the compiler will point out an error.

Guess you like

Origin blog.csdn.net/hebtu666/article/details/127204557