C++静态成员与友元

2016-11-14  本文已影响0人  帅碧

静态成员

静态变量的初始化(如何定义)

#include<iostream>
using namespace std;
class Student
{
    static int num;
public:
    Student()
    {
        num++;
    }
    ~Student()
    {
        num--;
    }
    int getnum()
    {
        return num;
    }
};
int Student::num=0;
int main()
{
    Student t1;
    Student t2;
    Student *t3=new Student;
    cout<<"Student num1 = "<<t2.getnum()<<endl;
    delete t3;
    cout<<"Student num2 = "<<t2.getnum()<<endl;
}
//结果为:
//Student num1=3;
//Student num2=2;

  • static int getnum()//静态成员函数
#include<iostream>
using namespace std;
class Student
{
    static int num;
public:
    Student()
    {
        num++;
    }
    ~Student()
    {
        num--;
    }
    static int getnum()
    {
        return num;
    }
};
int Student::num=0;
int main()
{
    cout<<"Student num0 = "<<Student::getnum()<<endl;
    Student t1;
    Student t2;
    Student *t3=new Student;
    cout<<"Student num1 = "<<Student::getnum()<<endl;
    delete t3;
    cout<<"Student num2 = "<<Student::getnum()<<endl;
}
//结果为:
//num0=0;
//num1=3;
//num2=2;

  • 静态成员函数只能操作静态成员变量
  1. 第一种
#include<iostream>
using namespace std;
class Student
{
    static int num;
    int id;
public:
    Student()
    {
        num++;
    }
    ~Student()
    {
        num--;
    }
    int getnum()
    {
        int id=0;
        return num;
    }
};
int Student::num=0;
int main()
{
//  cout<<"Student num0 = "<<Student::getnum()<<endl;
    Student t1;
    Student t2;
    Student *t3=new Student;
    cout<<"Student num1 = "<<t2.getnum()<<endl;
    delete t3;
    cout<<"Student num2 = "<<t2.getnum()<<endl;
}

  1. 第二种( 静态成员函数只能操作静态成员变量)(推荐此用法)
#include<iostream>
using namespace std;
class Student
{
    static int num;
    int id;
public:
    Student()
    {
        num++;
    }
    ~Student()
    {
        num--;
    }
    int getid()
    {
        id=0;
        return id;
    }
    static int getnum()
    {
        return num;
    }
};
int Student::num=0;//静态成员变量的初始化,一定要写在外面
int main()
{
//  cout<<"Student num0 = "<<Student::getnum()<<endl;
    Student t1;
    Student t2;
    Student *t3=new Student;
    cout<<"Student num1 = "<<Student::getnum()<<endl;
    delete t3;
    cout<<"Student num2 = "<<Student::getnum()<<endl;
}

总结

  • 静态数据成员用得比较多的地方
  • 一个静态成员函数不能访问非静态成员变量

友元

#include<iostream>
using namespace std;
class Student
{
    int m_id;
public:
    Student(int id)
    {
        m_id=id;
    }
    friend void test(Student t);//所以要加声明;
};
void test(Student t)
{
    cout<<"id = "<<t.m_id<<endl;//是错误的,m_id是私有的,不能再外部访问
}
int main()
{
    Student t1(10);
    test(t1);
}
//结果为:id=10;

友类

#include<iostream>
using namespace std;
class Student
{
    int m_id;
public:
    Student(int id)
    {
        m_id=id;
    }
    friend class A;
};
class A
{
public:
    void test(Student t)
    {
        cout<<"id = "<<t.m_id<<endl;
    }
};

int main()
{
    Student t1(10);
    A a;
    a.test(t1);
}
//结果为:id = 10;

  • 友元不能传递
#include<iostream>
using namespace std;
class Student
{
    int m_id;
public:
    Student(int id)
    {
        m_id=id;
    }
    friend class B;
    friend class A;
};
class B
{
    friend class A;
}
class A
{
public:
    void test(Student t)
    {
        cout<<"id = "<<t.m_id<<endl;
    }
};

int main()
{
    Student t1(10);
    A a;
    a.test(t1);
}

//结果为:
//id=10;

  • 诡异的程序
#include<iostream>
using namespace std;
class Student;
class A
{
public:
    void test(Student t);
};
class Student
{
    int m_id;
public:
    Student(int id)
    {
        m_id=id;
    }
    friend void A::test(Student t);
};

void A::test(Student t)
{
    cout<<"id = "<<t.m_id<<endl;
}
int main()
{
    Student t1(10);
    A a;
    a.test(t1);
}

//结果为:
//id=10

友元函数

需要友元的原因

  1. 有时候,普通函数需要直接访问一个类的保护或私有数据成员。
  2. 需要友元的另一个原因是为了方便重载操作符的使用。
  3. 友元函数不是成员函数,它是类的朋友,因而能够访问类的全部成员。
  4. 在类的内部,只能声明它的函数原型,加上friend关键字.

友元的使用

  1. 在类里声明一个普通函数,标上关键字friend,就成了该类的友元,可以访问该类的一切成员。
  2. 一个类的成员函数可以是另一个类的友元。
上一篇 下一篇

猜你喜欢

热点阅读