友元函数

#include <iostream>

using namespace std;

class Array {
public:
    friend void run(Array *a);  
    // 如果是来自别的类,而不是全局函数的话,就得写成 : friend void A::run(Array *a);
    // 这是外面的一个函数,不是Array的,但是加上这句话以后,就能在外面访问Array的private成员 
    

    Array(int age);
private:
    int age;
    char name;
};

Array::Array(int age) {
    this->age = age;
}
// test.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Array.h"
#include <iostream>

using namespace std;

void run(Array *t) {
    cout << "age: " << t->age << endl;
}

int main()
{
    Array *t = new Array(412);
    run(t);

    system("pause");
    return 0;
}

注意:两个类不能相互包含 

 

posted @ 2018-07-05 16:47  Jary霸  阅读(115)  评论(0)    收藏  举报