C ++ Study Notes: the usage and many basic knowledge of extern "C", learning notes extern
Use of extern "C": compile Part of the Code according to the c compiler during C ++ compilation.
You can modify a function,
extern "C" int initializeWinsockIfNecessary();
You can also modify a large segment (usually written in C ):
#ifdef __cplusplusextern "C" {#endif....#ifdef __cplusplus}#endif
What are the differences between the following two forms?
#include
#include "file.h"
A: the former is used to search for and reference files from the standard library path. h (cannot find or find the current path, but can also be written as an absolute path but not good), while the latter searches for and references file from the current working path. h.
What is the purpose of const?
A: In C/C ++, (1) You can define const constants, and (2) modify the return values and parameters of functions;
Here we describe the function return value. In this scenario, when returning a reference, the caller does not want to modify the return value.
In C ++, you can also modify the definition body of a function and define the const member function of the class. Things modified by const are forcibly protected to prevent unexpected changes and improve program robustness.
What is the difference between const and # define?
Can define constants, but define is expanded during pre-compilation. Therefore, const is easier to use for variable type check and debugging.
Sizeof has some notes for C ++:
An empty class occupies 1 byte, and a single inherited empty class occupies 1 byte. The virtual inheritance involves a virtual pointer, so it occupies 4 bytes.
Sizeof does not calculate the memory occupied by the static variable (it should be the size of the computing class, and does not calculate the static variable in the class ).
What is the difference between pointer and reference?
A: Both pointers and references provide the function of indirectly manipulating objects.
(1) the pointer definition can not be initialized, but the reference must be initialized at the definition. It is bound to an object. Once bound, as long as the reference exists, it will always be bound to this object;
(2) differences in the assignment behavior: the pointer assignment is to direct the pointer to another object, while the reference assignment is to modify the object itself;
(3) There is a type conversion between pointers. The reference is divided into const references and non-const applications. Non-const references can only be bound to objects of the same type, the const reference can be bound to objects of different but related types or the right value.
What is the difference between a null pointer and a hanging pointer? Smart pointer?
A: a null pointer is a pointer assigned as NULL. It is a pointer to a dynamically allocated object. After the object is released, a suspension pointer is generated.
The NULL pointer can be deleted multiple times, and the program becomes unstable when the suspension pointer is deleted again;
The use of NULL pointer and suspension pointer is invalid, and may cause program crash. if the pointer is a null pointer, even though it is also a crash, but it is a predictable crash compared to the suspension pointer (if the pointer is released but not assigned NULL, it is called a wild pointer ).
To avoid hanging pointers, you can use a smart pointer to add a reference count to the object, set it to 1 during initialization, and release the object only after the last unreferenced operation.
In C ++, there is malloc/free. Why is there new/delete?
Since malloc/free is a library function rather than an operator and is not controlled by the compiler, you cannot impose the tasks for executing constructor and destructor on it. Therefore, to complete object initialization and destructor, C ++ also needs new/delete.
What are the basic concepts and features of object-oriented technology?
A: basic features: encapsulation, inheritance, and polymorphism.
Which member functions does C ++ empty class have by default?
Default constructor
Destructor
Copy constructor
Value assignment operator (operator =)
Operator &) (a pair of non-const operators and a const operator)
Of course, all these are generated only when needed. For example, if you have defined a class, but have never defined the class object or used function parameters of this type, nothing will happen. For example, if you have never assigned a value between objects of this type, operator = will not be generated.
I didn't see it at 17-27. I copied it at the bottom of this article.
What will be executed before the main function is executed? Can code be executed after execution?
1. the constructor of the global object is executed before the main function.
2. You can use atexit () to register one or more functions to exit the cleanup function. They are called at exit () or return according to the reverse order of registration. (Or on_exit (), but this function is not recommended .)
#include
int atexit(void (*function)(void)); //return 0 on success.
Note that when fork sub-processes, these functions will be inherited to sub-processes. After the exec series is successfully executed, all functions are cleared.
#include
void exit(int status);
Exit () exits the process normally and returns (status & 0377) to the wait () of the parent process. The status can be EXIT_SUCCESS or EXIT_FAILURE.
Exit () calls the functions registered by atexit ()/on_exit () in the order of outgoing stack (reverse order. Then flush and close all open stdio streams and delete the files created through tmpfile. If _ exit () is called in a cleanup function or the kill result is exited, the subsequent cleanup function and stdio will not be executed.
If the child process exits (), a SIGCHLD signal is sent to the parent process. If SA_NOCLDWAIT is set for the parent process, the signal is undefined.
If the child process exit (), the parent process is waiting for the status of the child process (wait () Series function), or the parent process sets SA_NOCLDWAIT or sets SIGCHLD to SIG_IGN, the child process exits immediately. If the parent process neither has wait nor is set to ignore the child process exit, the child process will become a zombie process (except for one byte exit status, nothing at all ), wait until the parent process wait can still get the status.
Here we also talk about the difference between exit () and _ exit (). _ exit () causes the program to "exit immediately" and does not call the function registered by atexit ()/on_exit. It disables all file descriptors opened by itself, but whether to flush stdio and whether to delete the files created by tmpfile are related to the specific implementation (that is, there is no explicit provision ).
#include
void _exit(int status);
Of course, _ exit () will close the file descriptor, so there may also be some delay (for example, close before writing). If you want to achieve the "immediate" goal, it may be helpful to call tcflush () before _ exit.
-- No test start --
Static data member and static member functions
Answer: static data member:
Static data members exist independently of any objects of the class. Each static data member is an object associated with the class and is not associated with the object of the class. Static data member (const
Except for static data members) must be defined externally in the class definition body. Unlike common data members, static members should be initialized during definition instead of through class Constructor (not in the initialization list.
Static member functions:
The Static member function does not have this parameter. It can directly access the static member of the class and cannot directly use non-static members. Because static members are not part of any object, they cannot be declared as const. Static member functions cannot be declared as virtual functions.
Whether the virtual function table in the polymorphism class is created at Compile-Time or Run-Time
Answer: The virtual function table was created during the compilation period. Each virtual function is organized into an array of virtual function entry addresses. The hidden Member of the object-virtual function table pointer is initialized at runtime-that is, when the constructor is called, which is the key to implementing polymorphism.
A parent class writes a virtual function. If the function that the subclass overrides is not added with virtual, polymorphism can also be implemented in the subclass space. Is there a function of the parent class, or the private variable of the parent class.
Answer: as long as the base class has declared the virtual keyword when defining the member function, when the derived class is implemented, the virtual keyword can be added or not added, without affecting the implementation of polymorphism. All variables of the parent class (except static) exist in the space of the Child class ).
-- No test end --
Does the inline function perform parameter type check during compilation?
A: inline functions are used to check the parameter type, which is an advantage of inline functions over macros.
There are 1, 2 ,.... An unordered Array up to n is used to calculate the sorting algorithm. The time complexity is O (n), the space complexity is O (1), and exchange is used, and only two numbers can be exchanged at a time.
A: Obviously, bucket sorting is used to reuse the subscript of the original array.
======================================
17. In the hierarchy of inheritance, why is the basic class destructor a virtual function?
A: The Compiler always calls class member functions based on the type. However, the pointer of a derived class can be safely converted to a pointer of a base class. In this way, when a base class pointer is deleted, C ++ calls the base class destructor instead of the derived class, regardless of whether the pointer points to a base class object or a derived class object. If you rely on the code of the destructor of the derived class to release resources without reloading the destructor, there will be resource leakage.
18. Why cannot constructors be virtual functions?
A: virtual functions use a virtual call method. A call is a mechanism that can work with only partial information. If you create an object, you need to know the exact type of the object. Therefore, the constructor cannot be a virtual function.
19. If the virtual function is valid, why not set all functions as virtual functions?
A: No. First, virtual functions have a cost. Because each virtual function object needs to maintain a virtual function table, a certain amount of system overhead is generated when using virtual functions, this is unnecessary.
20. constructor can be an inline function.
21. What is polymorphism? What is the role of polymorphism?
A: Polymorphism refers to pointing the pointer or reference of the base class type to an object of the derived type. Polymorphism is implemented through the virtual function mechanism.
The role of polymorphism is interface reuse.
22. What is the difference between heavy load and coverage?
A: A virtual function is a function of the base class that you want to redefine In the derived class. The method of redefining the base class virtual function is overwrite;
Overload allows multiple functions with the same name in the same scope. The parameter tables of these functions are different. The concept of overload does not belong to object-oriented programming. The Compiler modifies the names of functions with the same name based on different parameter tables of the function, and then these functions with the same name become different functions.
The confirmation of overload is determined at compilation, and it is static; the confirmation of virtual function is dynamic at runtime.
23. Public inheritance, protected inheritance, and private inheritance
A:
(1) In public inheritance, the derived class object can access the public members in the base class, and the member functions of the derived class can access the public and protected members in the base class;
(2) In private inheritance, the base class members can only be accessed by members of the directly derived class and cannot be inherited further;
(3) When the inheritance is protected, the members of the base class are only accessed by members of the direct derived class and cannot be inherited further.
24. Protected members of the base class can be accessed through the derived class object but cannot be modified when the public inherits the class.
25. In which situations can I only use constructors to initialize the list, but not assign values to initialize the list?
Answer: const Member, referencing Member
26. What is a virtual pointer?
A: The virtual pointer or virtual function pointer is the implementation details of the virtual function. Each object with a virtual function has a virtual pointer pointing to the virtual function table of this class.
27. How does C ++ prevent a class from being instantiated? When will the constructor be declared as private?
Answer: (1) define the class as an abstract base class or declare the constructor as private;
(2) class objects cannot be created outside the class. Only objects can be created inside the class.