frend function in class

看板Programming作者時間18年前 (2007/07/01 07:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
有一個程式 內容是說明將一個class內的function設定為friend 只是.. 我看不太懂這樣做的目的為何 因為既然設定為friend 那是不是就可以呼叫裡面的資料 那該怎麼呼叫呢 我把原始程式貼上來 煩請大大賜教 #include <iostream> using namespace std; class Spec { private: void speed(int power); }; class Car{ private: friend void Spec::speed(int); //friend function of class public: int num; double gas; //friend void Spec::speed(int); //friend function of class }; class car1:public Car { }; void Spec::speed(int power) { int motive=power; cout<<"--March--\n"<<"傳入為"<<motive<<endl; } int main() { Spec pow; Car March; March.num=200; March.gas=20.0; pow.speed(300); cout<<"號碼為"<<March.num; cout<<"Gas為"<<March.gas<<endl; system("PAUSE"); return 0;} -- ┌─────KKCITY─────┐─┐ 優質連線服務///!! bbs.kkcity.com.tw └─ KKADSL 帶你環遊全世界 └──From:140.109.139.90 ──┘ KKADSL http://adsl.kkcity.com.tw --
文章代碼(AID): #16Xk2k00 (Programming)
文章代碼(AID): #16Xk2k00 (Programming)