看板Programming
标 题frend function in class
发信站KKCITY (Sun Jul 1 06:43:05 2007)
转信站ptt!ctu-reader!ctu-gate!news.nctu!news.ntu!news.au!zoonews.ee.ntu!news
有一个程式
内容是说明将一个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
--