Common objects, common member variables, and common member functions ~

Source: Internet
Author: User
Tags gety

Some member functions change objects, while some member functions do not change objects.
For example:
Int point: Gety ()
{
Return yval;
}
When this function is called, The point object is not changed, and the following function changes the point object:
Void point: setpt (int x, int y)
{
Xval = X;
Yval = y;
}
To make the meaning of member functions clearer, we can add const to the function prototype of the member functions without changing the object:
 

 

  Class Point
{
Public:
Int getx () const;
Int Gety () const;
Void setpt (INT, INT );
Void offsetpt (INT, INT );
PRIVATE:
Int xval, yval;
};
 

Const member functions should be added with the const limitation in both the function prototype description and Function Definition:
 

 

  Int point: Gety () const
{
Return yval;
}
Class set {
Public:
Set (void) {card = 0 ;}
Bool member (const INT) const;
Void addelem (const INT );
//...
};
Bool set: Member (const int ELEM) const
{
//...
}

 

A non-constant member function cannot be called by a constant member object because it may attempt to modify the data member of a constant:
Const set S;
S. addelem (10); // invalid: addelem is not a constant member function.
S. Member (10); // correct
Except for this rule, constructor and destructor are never defined as constant members, but can be called by constant objects (automatically called ). They can also assign values to the data members of constants unless the data members are constants themselves.
Why do we need a const member function?
In the member functions defined by the class, some member functions do not change the data members of the class. That is to say, these functions are read-only functions, some functions need to modify the values of class data members. If you add the const keyword to all functions that do not change data members for identification, it can obviously improve the readability of the program. In fact, it can also improve the program's reliability. It has been defined as a const member function. Once it attempts to modify the value of the data member, the compiler will handle it by mistake.
Const member functions and const objects
In fact, the const member function has another function, that is, constant object correlation. For built-in data types, we can define their constants, and user-defined classes can also define their constant objects. For example, the method for defining an integer constant is:
Const int I = 1;
You can also define a constant object. Assume that there is a class classa, and the method for defining the constant object of this class is:
Const classa A (2 );
Here, a is a const object of the classa class, And the constructor parameter passed to it by "2. The data member of the const object cannot be changed during the life cycle of the object. However, how can we ensure that the data members of this class are not changed?
To ensure that the data member of the const object is not changed, in C ++, the const object can only call the const member function. If a member function does not actually modify the data member in any form, but it is not limited by the const keyword, it cannot be called by a constant object. Here is an example to illustrate this problem:
 

 

  Class C
{
Int X;
Public:
Int getx ()
{
Return X;
}
Void setx (int x)
{
This-> X = X;
}
};
Void main ()
{
Const C constc;
Cout <constc. getx ();
}

 

If we compile the above program code, the compiler will encounter an error message: constc is a constant object, which can only call the const member function. Although the getx () function does not actually change the data member X, it cannot be called by the constc object because there is no const keyword restriction. If we use the above bold code:
Int getx ()
Rewrite:
Int getx () const
Then re-compile the program.
Use of const member functions
The const member function indicates that the member function can only read class data members, but cannot modify class member data. When defining a const member function, place the const keyword between the parameter table of the function and the function body. Some may ask: why not place const before function declaration? This means that the return value of the function is a constant and the meaning is completely different. The following is an instance that defines the const member function:
Class X
{
Int I;
Public:
Int F () const;
};
The const keyword must be repeated in function implementation in the same way; otherwise, the compiler will regard it as a different function:
Int X: F () const
{
Return I;
}

 In other words, const must be used to declare and define common member functions. Otherwise, the compiler considers these two different functions as overload. A natural question is, why is this considered an overloaded function? Will the compiler be confused during calls? With this problem, I did the following tests.


Struct Test
{
   Test (INT ){};
   Test (){};
   Void fun () const;
   Void fun ();
};
Void test: Fun () const
{
   Printf ("const fun! \ N ");
}
Void test: Fun ()
{
   Printf ("fun! \ N ");
}

Int main (INT argc, char * argv [])
{
   Const Test;
   A. Fun ();
   Test B;
   B. Fun ();
   Return 0;
}

Output result:

Const fun!

Fun!

The experimental results show that the common and common member functions can coexist in the form of overloading. It depends on the attributes of the caller. If the caller is a common object or function, the system first calls the common member function. Otherwise, the system calls the common member function. What is the purpose of this design? Not clear.

If F () tries to change I in any way or call another non-const member function, the compiler will give an error message. Any function that does not modify member data should be declared as a const function, which helps improve the readability and reliability of the program.

  Object. member functions

    Object         Member Functions     Right/wrong
1, Const       Const         Pair
2, Const       Non-const     Error
3, Non-const   Const         Pair
4, Not-const   Non-const     Pair

          Member functions call member functions

    Member Functions     Member Functions     Right/wrong
5, Const       Const         Pair
6, Const       Non-const     Error
7, Non-const   Const         Pair
8, Non-const   Non-const     Pair

 

 

The const is added to the member function of the class, indicating that this function will not make any changes to the data member of the Class Object (accurately non-static data member.

When designing a class, a principle is that const must be added to all member functions that do not change data members. Const cannot be added to member functions that change data members. Therefore, the const keyword makes more explicit limits on the behavior of member functions: A member function with const modification (that is, the const is placed behind the function parameter table, rather than before or within the function table ), only data members can be read, but data members cannot be changed. Without the const-modified member functions, data members can be read and written.

In addition, what are the benefits of adding const to the member functions of the class? That is, a constant (that is, a const) object can call the const member function, rather than a non-const modified function. Just as data of non-const type can assign values to the variable of const type, otherwise, it is not true.

 

 

 

 

-----------------------

By the way, you can also modify the member variables that can be modified by mutable in a common function.

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.