Negative tive C ++ notes two member functions modified by const_and_non-const

Source: Internet
Author: User

 

 

1. There are two points of view for the const modified member functions: logic constness and bitwise constness.

Bitwise constness's point of view is: "It does not change any bit of the object". In this way, the compiler only needs to assign values to the member variables. This is the definition of read-only. Therefore, the const member function cannot change any non-static member variable in the object.

Eg1:

Class TextBlock

{

Public:

............

Char & operator [] (std: size_t position );

Const char & operator [] (std: size_t position) const;

Private:

Std: string text;

};

 

TextBlock bt ("Jack ");

Std: cout <bt [0]; // true, read a non-const TextBlock

Bt [0] = 'T'; // true, write a non-const TextBlock

Const TextBlock cbt ("Rose ");

Std: cout <cbt [0]; // true, read a const TextBlock

Cbt [0] = 'l'; // false, cannot write a const TextBlock !!

Char * ptr = & ctb [0];

* Ptr = 'T'; // true, not change the adress of ctb [0], but the content has changed !!

 

Logical constness's point of view is that it can change some bits in the object, for example, * ptr = 'T' in eg1. This will change the pText value in ctb !!

 

 

If a common member function of a class has a non-const data member and requires a value assignment, you can only use mutable to modify the non-const data member.

Eg2:

Class TextBlock

{

Public:

TextBlock (char * str = "\ 0"): pText (str), textLength (0), lengthIsValid (false)

{}

Std: size_t length () const;

Private:

Std: size_t textLength;

Bool lengthIsValid;

};

Std: size_t TextBlock: length () const

{

If (! LengthIsValid)

{

TextLength = std: strlen (pText );

LengthIsValid = true;

}

Return textLength;

}

 

2. Avoid repeated const and non-const member functions. In this case, remove the read-only const and add the read-only const.

Eg3:

Class TextBlock

{

Public:

TextBlock (string name = "\ 0 ");

Const char & operator [] (std: size_t position) const; // common member function

Char & operator [] (std: size_t position); // common member function

Private:

Std: string m_Name;

};

 

 

TextBlock: TextBlock (string name): m_Name (name)

{}

Const char & TextBlock: operator [] (std: size_t position) const

{

If (position> = m_Name.length () | position <0) // border check

{

Throw 0; // throw an exception

}

Else

{

Return m_Name [position];

}

}

 

 

 

 

// Use common member functions to call common member functions

Char & TextBlock: operator [] (std: size_t position)

{

// Static_cast <const TextBlock &> converts a non-object type to a common object to call a common member function.

// Cosnt_cast removes the return constant of operator []

// If two conversions are not performed in this way, the non-const operator [] function will be called recursively infinitely.

Return const_cast <char &> (static_cast <const TextBlock &> (* this) [position]);

} Www.2cto.com



From Cql_liliang's Blog

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.