Sharing data protection

Sharing of data protection

Often the object

Often the object is a special on the image: its data member values can not be changed during the existence of the entire object. That is often the object must be initialized, and can not be updated .

class A
{
    public:
    A(int x,int y):x(i),y(j){}

    private:
    int x,y;

}
const A a(3,4);

Such a modification is a constant const object with the object when the object needs to define its initial value often, and often the initial value with the object and does not change operation of program

Modified by const class members

Often a member function

Declaratively:
type specifier function name (parameter list) const;
constitute an effective when applied as overloads at const member functions normally function overloading the conventional function the same naming:

void print();
void print() const;
#include<iostream>
using namespace std;

class R{

public:
    R(int r1, int r2) :r1(r1), r2(r2){ };
    void print();
    void print() const;
private:
    int r1, r2;
};
void R::print(){

    cout << r1 << ":" << r2 << endl;
}

void R::print() const{

    cout << r1 << ":" << r2 << endl;
}
int main()
{
    R a(5, 4);
    a.print();
    const R b(8, 10);
    b.print();
    return 0;

}

Often object can only call its regular member functions, but can not call other member functions (protection of regular objects on the c ++ syntax mechanism)
, whether by regular object calls often a member function, the target objects are apparent during the call often a member function often make changes to objects, which often can not be a member function of the target object's data members

Often data members

Often once declare and initialize data members, functions will not be any of the members of the assignment, the constructor to initialize the data members, only through initialization list.

#include<iostream>
using namespace std;

class A{

public:
    A(int i);
    void print() ;
private:
    const int a;
    static const int b;
};
const int A::b = 10;//给静态常数据成员赋值

A::A(int i) :a(i){} // 构造函数

void A::print(){

    cout << a << ":" << b<< endl;
}

int main()
{
    A a1(100),a2(0);//给常数据成员赋值
    a1.print();
    a2.print();
    
    return 0;

}

Often quoted

If you use the const modifier when declaring references, reference is declared is often quoted, often cited in reference to the object can not be updated ;
if used often make reference parameter, then change to the argument does not happen accidentally.
Often quoted statement:
const type specifier & reference name;

#include<iostream>
using namespace std;

class Point{

public:
    Point(int x = 0, int y = 0) :x(x), y(y){}
    int getx(){ return x; }
    int gety(){ return y; }
    friend  float dist(const Point &p1, const Point &p2);//常对象引用
private:
    int x, y;
};
//常对象引用作形参
float dist(const Point &p1, const Point &p2){
    double x = p1.x - p2.x;
    double y = p1.y - p2.y;

    return static_cast<float>(sqrt(x*x + y*y));

}

int main()
{
    const Point myp1(1, 1), myp2(4, 5);
    cout << "The distance is:" << endl;
    cout << dist(myp1, myp2) << endl;

    return 0;

}

For its function without changing the parameter values, traditional values ​​should not be used in the normal way, because that will make the object can not be passed, the value passed or often cited way to avoid this problem; the copy constructor parameter passing generally normally-reference.

Guess you like

Origin www.cnblogs.com/wxllovezn/p/11609075.html