Sharing and protecting data

Sharing and protecting data

1. Visibility

Seen from the concept of the reference identifier to talk in terms of showing what can see when viewed from the inner to the outer scope scope. If an identifier is declared in the outer layer, and is not declared in the inner layer, the identifier is viewable in the inner layer; if a declaration with the same identifier in the inner layer, the outer layer identifier the inner layer is not visible.

#include<iostream>
using namespace std;
int main(){
int i=5;
{
int i = 7;
cout<<i<<endl;//输出为7
}
cout<<i<<endl;//输出为5
}

2. The lifetime of a variable

• Static survival
began to persist until the end of the program, such as variables with static function declarations from the variable declaration. (Note that the static global variable limiting its scope, that is only valid within the definition of that variable in the source file, it can not use the same files in the other sources in the source program.)

#include<iostream>
using namespace std;
void test(){
static int i=1;
i++;
cout<<i<<endl;
}
int main(){
test();
test();
test();//三次 调用test函数观察输出的i值
return 0;
}

Dynamic survival
is not modified with a static variable declaration block scopes with dynamic survival. In a statement at the beginning, and finally the identifier at the end of the scope.

#include<iostream>
using namespace std;
int main(){
{
int i=1;
cout<<i<<endl;
}//i的生存期结束
cout<<i<<endl;//此处报错 
return 0;
}

3. static member class

    1. C++中可以定义静态成员变量和静态成员函数

    2. 静态成员属于整个类所有,不需要依赖任何对象。

    3. 可以通过类名直接访问public 静态成员

    4. 可以通过对象名访问public静态成员
    

Static member function can directly access static members static members are not part of the object, but part of a class, which is the most essential difference between static members and ordinary members.

1. The use of static member variables

#include<iostream>
using namespace std;

class Point {
public:
    Point(int x = 0, int y = 0) :x(x), y(y) {//构造函数
        count++;
    }
    Point(Point& p) {  //复制构造函数
        x = p.x;
        y = p.y;
        count++;
    }
    ~Point() { count--; }

    int getX() { return x; }

    int getY() { return y; }

    void showcount() {  
        cout << "object count:" << count;
    }
private:
    int x;
    int y;
    static int count;//静态成员变量
};
int Point::count = 0;

int main() {
    Point a(4, 5);
    cout << "Point a:" << a.getX() << " " << a.getY() << endl;
    a.showcount();
    cout << endl;
    Point b(a);
    cout << "Point b:" << a.getX() << " " << a.getY() << endl;
    b.showcount();
    return 0;

}

2. Use a static member function

#include<iostream>
using namespace std;

class Point {
public:
    Point(int x = 0, int y = 0) :x(x), y(y) {//构造函数
        count++;
    }
    Point(Point& p) {  //复制构造函数
        x = p.x;
        y = p.y;
        count++;
    }
    ~Point() { count--; }

    int getX() { return x; }

    int getY() { return y; }

    static void showcount() {  //静态成员函数
        cout << "object count:"<<count;
    }
private:
    int x;
    int y;
    static int count;
};
 int Point::count = 0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

int main() {
    Point a(4, 5);
    cout <<"Point a:"<< a.getX ()<< " " << a.getY()<<endl;
    Point::showcount();
    cout << endl;
    Point b(a);
    cout << "Point b:" << a.getX() << " " << a.getY() << endl;
    Point::showcount();
    return 0;

}

4. Class friend

In some cases, allow certain non-member functions to access private members of a class, while still preventing general access, which is very easy to do. E.g. overloaded operators, such as input or output operators, often need to access the private data members of the class.

1. The friend function
friend (frend) mechanism allows a class to grant the specified function or class of its access to non-public member, friend to friend declaration start, it can only appear inside a class definition, friend declarations can appear anywhere in the class: the friend was not made to members of that class friendship is, so accessing them is not part of his statement appears to control the impact. Typically, the friend declaration as a group on the beginning or end of the class definition is a good idea.

Friend function is a function of some, though not all members of a class member function was able to access the class. Class grant it access to special friend so the friend function will be able to access to all members of the class.

#include <iostream>

using namespace std;

class A
{
public:
    friend void set_show(int x, A &a);      //该函数是友元函数的声明
private:
    int data;
};

void set_show(int x, A &a)  //友元函数定义,为了访问类A中的成员
{
    a.data = x;
    cout << a.data << endl;
}
int main(void)
{
    class A a;

    set_show(1, a);

    return 0;
}

2. Tomomoto类

    友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。当希望一个类可以存取另一个类的私有成员时,可以将该类声明为另一类的友元类。

About friend class considerations:

(1) Friendship is not inherited.

(2) friend relationship is unidirectional, not commutative. If class B is a friend of class A, class A is not necessarily a friend of class B, depending on whether there is a corresponding statement in the class.

(3) 友元关系不具有传递性。若类B是类A的友元,类C是B的友元,类C不一定是类A的友元,同样要看类中是否有相应的申明。

#include <iostream>

using namespace std;

class A
{
public:
    friend class C;          //这是友元类的声明
private:
    int data;
};

class C             //友元类定义,为了访问类A中的成员
{
public:
    void set_show(int x, A &a) { a.data = x; cout<<a.data<<endl;}
};

int main(void)
{
    class A a;
    class C c;

    c.set_show(1, a);

    return 0;
}

Guess you like

Origin www.cnblogs.com/pikapk/p/11610232.html