C + + Interview Summary (2)

Source: Internet
Author: User

4. Deep copy and shallow copy

(1) When will the copy function be used?

An object is passed into the function as a value (that is, as an input parameter)

An object is returned from the function as a value (that is, as a return value)

An object needs to be initialized with another object

(2) Whether the copy function should be customized

If you do not define the copy function yourself, then the compiler will automatically generate a default copy function that will complete the bit copy between the objects.

(3) Understanding of deep copy and shallow copy

When a class has a resource (such as a pointer), the resource is reassigned when the object of the class is copied (pointing to a piece of memory, whether a memory is being re-applied when the copy occurs), the redistribution is a deep copy, not a shallow copy,

http://blog.csdn.net/feitianxuxue/article/details/9275979 This everyone can take a closer look at deepening understanding.

5. Rewrite the CString (this is also the basic HKCEE)

Class String
{
Public
String (const char *STR = NULL); General constructors
String (const string &another); Copy constructor
~string (); Destructors
string& operater = (const String &RHS); Assignment function
Private
char* m_data; Used to save strings
};

string::string (const char *STR = NULL)//General constructor

{

if (NULL==STR)

{

m_data = new Char[1];

M_data[0] = ' + ';

}else

{

m_data = new Char[strlen (str) +1];

strcpy (M_DATA,STR);

}

}

string::string (const String &another)//copy constructor

{

m_data = new Char[strlen (anoter.m_data) +1];

strcpy (M_data,another.m_data);

}

string& String::operater = (const String &RHS)//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 Issues

Thread synchronization methods: Critical section, mutex, semaphore, event.

Critical section: Through the serialization of multithreading to access public resources or a piece of code, fast, suitable for controlling data access.
Mutex: Designed to coordinate separate access to a shared resource together.
Semaphore: Designed to control a limited number of user resources.
Event: Used to notify a thread that some events have occurred, thus initiating the start of a successor task.

(1) What is the difference between the critical area and the mutual exclusion amount?

The mutex is similar to the critical section, and only the line friend with the mutex has permission to access the resource, because there is only one mutex object, so it is determined that the shared resource will not be accessed by multiple threads at the same time in any case. The thread that currently occupies the resource should hand over the owning mutex after the task has been processed so that other threads can access the resource after it is acquired. The mutex is more complex than the critical section. Because using mutexes not only enables the secure sharing of resources in different threads of the same application, but also enables secure sharing of resources between threads of different applications.

Ext.: http://www.cnblogs.com/lbdada/p/5765424.html

(turn) C/C + + interview Summary (2)

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.