(31.2) often objects, often members, often pointer, often quoted

1. Why do we need the data is defined as read-only?

  • C ++ has a lot of security measures to protect data, such as data members and other private protection class. But for some common data, such as the function of a real parameter participating like, we can access the same data object by different routes at different occasions. Sometimes inadvertently misuse will change the value of the data, which is that people do not want to appear.
  • Both to make the data can be shared among the function, but also to ensure that it is not to be edited, the const can be defined, i.e. the data is defined as read-only.

2. Often objects

  • Const is used when defining the object is defined, the object is often called, is defined by the general form:
类名 const 对象名1(实参列表), 对象名2,......;
或者
const 类名 对象名1(实参列表), 对象名2,......;

eg:如已定义Data类, 定义常对象如下:
const Data d1; //定义常对象
Data const d2(10,20,100); //定义常对象
  • Data members often objects are const, and therefore must have the initial value.
    Whatever the case, the data members often object can not be modified.
  • In addition to the default constructor and destructor default synthesis, we can not call the non-const member functions normally type object.
    E.g:
d1.data=10; //错误, 常对象数据成员data为const, 不能成为左值
d2.show(); //错误, 不能调用常对象中非const型成员函数
  • In actual programming, sometimes you must modify the value of a data member often object, then the data members can be declared as mutable (variable) to modify its value.
    Declaration form:
mutable 数据成员类型 数据成员名列表; //可变的数据成员声明
其中mutable为C++关键字, 表示可变的数据成员。
eg:
mutable int data; //可变的数据成员

2. Often data members

  • Const defined using data member declaration, often called data members.
    The general form of the statement is as follows:
const 数据成员类型 数据成员名列表; //常数据成员声明

eg:
const int data;//常数据成员声明
  • Whether member function or a non-member functions are often not allowed to modify the value data member.
  • Often data members can only be initialized by the constructor initializer list.

3. regular member functions

  • When defining the const member functions defined, often called member function.
    The general form is defined as:
class 类名 
{ //类体
	…
	返回类型 函数名(形式参数列表) const //常成员函数定义
	{
		函数体
	}};

或者
class 类名 
{ //类体
	…
	返回类型 函数名(类型1 参数名1,类型2 参数名2,) const;};
返回类型 类名::函数名(形式参数列表) const //常成员函数外部定义
{
	函数体
}

eg://const要写在声明和定义处
int getx() const; //类体声明常成员函数
int Data::getx() const //外部定义常成员函数
{
	return x;
}

需要注意const的位置在函数头和函数体之间, 不要写成:
const 返回类型 函数名(类型1 参数名1,类型2 参数名2,);//这种写法表示函数返回值只读(如返回只读引用) 。
  • Whether a statement is often defined member function must have const keyword.
    Often const member functions can access the data members, can also access non-const data member.
    const data members can be accessed by const member functions can also be accessed by non-const member functions.
    Circumstances below <restrict access relations const> Table:
数据成员 						非常成员函数(普通数据成员) 				常成员函数
非常数据成员(普通数据成员) 										允许访问,可以修改 允许访问,不能修改
常数据成员 							允许访问,不能修改 					允许访问,不能修改
常对象数据成员 						不允许访问和修改 					允许访问,不能修改
  • Description of the Constant member function
    (1) in a class, if the value of some data member allows to modify the value of other data members can not be modified, it may be a part of the data members declared const (constant data members), such that value can not be modified. The ordinary member functions can modify the ordinary data members, but often only have access to the value of data members.
    (2) If the required value of all the data members are not allowed to change, the object can be declared as const (often the object), you can only use const member functions to access data members, and can not modify its value. In this way, data members will not be modified in any case.
    (3) If the definition of a regular objects, which can only call const member functions, and can not call non-const member functions. If you need to access the data members of the object, the object can be often all member functions are declared const member functions, but should ensure that no member objects to modify data in the function.
    (4) member function normally object is not necessarily constant member function. If the member function normally object not add const statement, C ++ handle it as a very member function.
    (5) can not call another member function normally very member function.

4. The constant pointer pointing object

  • Pointer pointing object often form defined as follows:
  • Typically, often used as a pointer to the function parameter, the purpose is not allowed to change the value of the pointer variable in the function is being executed, always point to the original object. If you try to modify the value of the constant parameter pointer function is being executed, a compile error occurs.
类名 * const 指针变量名=对象地址; //常指针
  • The implication is that such a pointer always maintains its initial value, the program can not modify its point.
    eg:
Data d(10,20,100), d1;
Data *const p=&d; //定义指向对象的常指针
p=&d1; //错误, 不能修改常指针的指针值

请注意, 对象的常指针必须在定义时初始化, 因为其后就不能再指向别的对象了。
虽然常指针是const的不能改变指向, 但常指针所指向的对象却不一定是const的。

The pointer variable pointing to an object often

  • Often the object pointer points to form a variable defined as follows:
const 类名 * 指针变量名;
  • The implication is that the object is a pointer variable to point const (i.e. normally object).
    eg:
const Data *p; //指向常对象的指针变量

指针p的指向却是可以改变的
  • Often the object pointer points to a variable, it can not be changed by the value of the object pointed to, but the value of the pointer variable itself can be changed, and therefore may not be initialized at definition.

  • Please note the difference between frequently pointed object pointer variable and often pointing to an object pointer variable:

Data *const p=&d; //定义指向对象的常指针  指针的指向是不能改变的
const Data *p; //指向常对象的指针变量    不能通过指针指向其它对象的值的

(1)如果一个对象已被声明为常对象, 只能用指向常对象的指针变量指向它。

(2)如果定义了一个指向常对象的指针变量, 即使它指向一个非const的对象, 其指向的对象也是不能通过指针来改变的。
(指向常对象的指针变量可以指向常对象,也可以指向一个非常对象,但是都不能通过该指针去改变其指向对象的值)

(3)指向常对象的指针常用作函数形参, 目的是在保护形参指针所指向的对象, 使它在函数执行过程中不被修改。

6. often referenced object

  • In the C ++ program, often used often and frequently the object pointer for the referenced function parameters. This will not only ensure data security, the data can not be freely modified in the function, but also in time and do not have to call the function argument passed a copy of the object, dramatically reducing the cost of space and time function calls.
  • Object references often form defined as follows:
const 类名 & 引用变量名;
  • For example, using the copy constructor will often cited as a function of the sole parameter:
Data(const Data& r) : x(r.x),y(r.y),data(r.data) { } //复制构造

//只能作为传递过来对象的引用,别名
  • eg:
#include <iostream>
#include <string.h>
using namespace std;
class A
{
	public:
		int getArea() const  //常成员函数不能改变数据成员的值
		{
			return w*h;
		}
		void setWH(int x, int y) // 因为该成员函数修改了数据成员的值,所以不能被定义为常成员函数
		{
			w=x;
			h=y;
		}
		A(int x,int y)
		{
			w=x;
			h=y;
		}
		A(){}
	private:
		int w,h;
};

int main()
{
	A a;//普通对象可以不初始化
	a.setWH(3,9);//a可以调用非常成员函数
	A const b;//错误, 常对象必须声明的同时初始化, 正确的是:A const b(3,6);
	
	b.setWH(3,7);//错误, 常对象不能调用非常成员函数    常对象只能调用常成员函数
	cout<<a.getArea()<<endl<<b.getArea()<<endl;

	return 0;
}



Published 510 original articles · won praise 134 · Views 150,000 +

Guess you like

Origin blog.csdn.net/u011436427/article/details/103882861