Difference between a copy constructor and an assignment constructor (operator=) __ function

Source: Internet
Author: User
Tags assert
difference between a copy constructor and an assignment constructor (operator=)

For the copy constructor and the definition of the assignment constructor, I'm not going to nag, or give a simple example, more intuitive.

Class CStr
{

Public

CStr (); Default constructor

CSTR (const char* psz); A generalized copy constructor, but some people disagree with my opinion

CSTR (const cstr& str); Copy Constructors

Const cstr& operator= (const cstr& str); Assignment constructors

size_t getsize () const; What does the const mean here? Does it have to be.

Operator Const char* () const {return m_pdata;}

Protected
Const cstr* _copy (const cstr& str);

Private
char* m_pdata;
size_t m_size;
};

Cstr::cstr ()
{
M_pdata = NULL;
m_size = 0;
}

size_t cstr::getsize () const
{
return m_size;
}

Const cstr* cstr::_copy (const cstr& STR)
{

if (this!= &str)

{

if (m_pdata) {

Delete[] M_pdata;

}

M_size = str. GetSize ();

M_pdata = new Char[m_size + 1]; ASSERT (M_pdata);

strcpy (M_pdata, str);
}

return this;
}

CSTR::CSTR (const char* psz): M_pdata (NULL), m_size (0)

{

ASSERT (PSZ);

if (m_pdata!= psz)

{

if (m_pdata) {

Delete[] M_pdata;
}

M_size = strlen (psz);

M_pdata = new Char[m_size + 1]; ASSERT (M_pdata);

strcpy (M_pdata, psz);

}
}

CSTR::CSTR (const cstr& str): M_pdata (NULL), m_size (0)
{
_copy (str);
}

Const cstr& cstr::operator= (const cstr& STR)
{
return * (_copy (str));
}

int main ()
{
Const char* PSZ = "Test";
Const char* PSZ1 = "Me";
CStr str (PSZ);     Copy constructor, which is called CStr (const char* psz). #1
CStr str2 (str); Copy constructor, which is called CSTR (const cstr& str) #2
CStr str1 = psz1;    Copy constructs, str1 didn't exist before, and now we have to construct it. #3
str = STR1; The true assignment constructor #4
return 0;
}

This small example above, is what I've just written for you, this example is mainly to illustrate the difference between the assignment constructor and the copy constructor, in fact, my view is that the assignment constructor should not be called a constructor, at best can be computed as a single overloaded operator function.

In C, we know that "= =" is a sentence operator, and "=" is an assignment operator, so as long as there is no "=" operator, there will be no assignment operation to speak.

I think that the assignment constructor should not be called a constructor because when the assignment constructor is invoked, the class object already exists, naturally not the constructor class object, it is simply modifying the member variable of the existing class object (the so-called assignment) operation. The copy constructor constructs a new class object using an existing class object.

Back to the example above, I believe that most people should be able to distinguish between the #2, the #4所调用的函数, and for #3, I would like to highlight that the actual intention of the assignment construct is to modify an existing object, and now STR1 is not defined, So here you have to call the copy constructor to produce a CStr object first.

Finally, remember that when a definition is performed at the same time as the assignment, it must be defined first, so the copy constructor must be called first, and the actual invocation assignment constructor is the statement that conforms to the simple assignment syntax (that is, an expression with only two operands). If you have any questions, you can ask me, I will try to explain to you.

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.