C/c ++ interview Summary (2) Interview Summary
4. Deep copy and light copy
(1) When will the copy function be used?
An object is passed in a function as a value (that is, as an input parameter)
An object is returned from a function by passing values (that is, as the return value)
One object needs to be initialized through another object
(2) Whether to customize the copy Function
If you do not define a copy function, the compiler automatically generates a default copy function, which completes the bitwise copy between objects.
(3) Understanding of deep copy and light copy
When a class has resources (such as pointers), whether the resources are re-allocated when the class objects are copied (the pointer points to a piece of memory, whether a copy request will be re-applied for a piece of memory,
Bytes.
5. Rewrite CString (this is also a basic test)
Class String
{
Public:
String (const char * str = NULL); // common Constructor
String (const String & another); // copy the constructor
~ String (); // destructor
String & operater = (const String & rhs); // value assignment function
Private:
Char * m_data; // used to save strings
};
String: String (const char * str = NULL) // common Constructor
{
If (NULL = str)
{
M_data = new char [1];
M_data [0] = '\ 0 ';
} Else
{
M_data = new char [strlen (str) + 1];
Strcpy (m_data, str );
}
}
String: String (const String & another) // copy the constructor
{
M_data = new char [strlen (anoter. m_data) + 1];
Strcpy (m_data, another. m_data );
}
String & String: operater = (const String & rhs) // value assignment function
{
If (& rhs = this)
{
Return * this;
}
Delete [] m_data;
M_data = NULL;
M_data = new char [strlen (rhs. m_data) + 1];
Strcpy (m_data, rhs. m_data );
Return * this;
}
String ::~ String () // destructor
{
Delete [] m_data;
M_data = NULL;
}
6. thread synchronization problems
Thread Synchronization Methods: critical section, mutex, semaphore, and event.
Critical section: accesses public resources or code segments through multi-thread serialization, which is fast and suitable for controlling data access.
Mutex: designed to coordinate separate access to a shared resource.
Semaphore: designed to control a limited number of user resources.
Event: it is used to notify the thread that some events have occurred and start subsequent tasks.
(1) What is the difference between the critical section and mutex?
The mutex is similar to that in the critical section. Only threads with mutex objects have the permission to access resources. Because there is only one mutex object, therefore, it is determined that the shared resource will not be accessed by multiple threads at the same time under any circumstances. The thread occupying the resource should hand over the mutex object after the task is processed, so that other threads can access the resource after obtaining it. Mutex is more complex than that in the critical section. Because mutex can not only achieve secure resource sharing in different threads of the same application, but also achieve secure resource sharing among threads of different applications.