怎么做网站关键词库排名,网页设计师中级证书有用吗,微网站的好处,定制网站系统开发欢迎转载#xff0c;但请标明作者 “九天雁翎”#xff0c;当然#xff0c;你给出这个帖子的链接更好。 呵呵#xff0c;又来了#xff0c;自从我开始尝试描述类以来#xff0c;我发现我自己是开始真的了解类了#xff0c;虽然还不到就明白什么叫oo的高深境界#xff0… 欢迎转载但请标明作者 “九天雁翎”当然你给出这个帖子的链接更好。 呵呵又来了自从我开始尝试描述类以来我发现我自己是开始真的了解类了虽然还不到就明白什么叫oo的高深境界起码对于类的使用方法了解的更多了希望你看了以后也能有所进步啊 现在开始讲一个有利有弊的东西友元(friend),我以前讲过了private的数据和函数别人是不能直接调用的这一点对于封装起到了很重要的作用。但是有的时候总是有调用一个类private成员这样需要的那怎么办呢C给了我们友元这个家伙按我的习惯首先看个例子。当然还是我们的水果类 例5.1 #include string #include iostream using namespace std; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 friend bool isSame(Fruit ,Fruit ); //在这里声明friend友元函数 public: void print() //定义一个输出名字的成员print() { coutcolour nameendl; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst){} //构造函数 Fruit(){} }; bool isSame(Fruit aF,Fruit bF) { return aF.name bF.name; //注意这个函数调用了Fruit的private数据本来可是不允许的 } int main() { Fruit apple; Fruit apple2(apple); Fruit orange(orange,yellow); coutapple orange ?: isSame(apple,orange)endl; coutapple apple2 ?: isSame(apple,apple2)endl; return 0; } 这里我们声明了一个isSame检测是否同名的函数而且这不是Fruit类的一个函数虽然他在类里面声明了怎么看出来假如是类的成员函数在外部定义必须要Fruit::这样定义不是吗isSame()没有这样他是个独立的函数但是他可以调用Fruit类的私有成员因为在类里声明了他是Friend的这就像你告诉保安编译器某某isSame)是你的朋友friend)然后让他可以进入你的家调用私有成员一样别人就不允许非友元函数不允许这样说够明白吗你可以尝试去掉friend声明看看编译错误。证明friend的作用我这里的得出的编译错误是这样error C2248: Fruit::name : cannot access private member declared in class Fruit也就是name是私有成员不能调用。不仅可以声明友元函数还可以声明友元类。效果类似。看下面的例子。 例5.2 #include string #include iostream using namespace std; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 friend class Person; //声明一个友元类Person public: void print() //定义一个输出名字的成员print() { coutcolour name; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst){} //构造函数 }; class Person //定义类Person { string likedFruit; public: string name; bool isLike(Fruit aF) { return likedFruit aF.name; //注意他调用了Fruit类的私有成员这本来是不允许的 } Person(const string npe jim,const string lF apple):name(npe),likedFruit(lF){} }; int main() { Fruit apple; Fruit orange(orange,yellow); Person jim; coutIs jim.namelike ; apple.print(); cout? : jim.isLike(apple)endl; //看看这个函数的调用 return 0; } bool isSame(Fruit aF,Fruit bF,Fruit cF) { return (aF.name bF.name)(bF.name cF.name); } 具体Person类和程序到底是什么意思我想只要你看了我以前写得东西应该很明白了就不多注释和讲了我现在主要是讲友元(friend)的用途。另外一点重载函数的话你想让几个成为友元就让几个其他的将不是友元函数这里提醒一下重载函数其实可是各自独立的函数只不过在C中为了调用方便让他们叫同一个名字而已。你不相信可以自己试试。比如说在例5.1中假如你重载一个但是却不声明为友元编译是通不过的。你必须这样各自声明。见下例。 例5.3 #include string #include iostream using namespace std; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 friend bool isSame(Fruit aF,Fruit bF); //声明为友元 friend bool isSame(Fruit aF,Fruit bF,Fruit cF); //再次声明为友元 public: void print() //定义一个输出名字的成员print() { coutcolour name; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst){} //构造函数 }; bool isSame(Fruit aF,Fruit bF) { return aF.name bF.name; } bool isSame(Fruit aF,Fruit bF,Fruit cF) { return (aF.name bF.name)(bF.name cF.name); } int main() { Fruit apple; Fruit apple2; Fruit apple3; Fruit orange(orange,yellow); coutisSame(apple,apple2)isSame(apple,apple2,orange)endl; return 0; } 现在再回过来看例4.0。 #include string #include iostream using namespace std; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 public: bool isSame(const Fruit otherFruit) //期待的形参是另一个Fruit类对象测试是否同名 { return name otherFruit.name; } void print() //定义一个输出名字的成员print() { coutcolour nameendl; } Fruit(const string nst,const string cst green):name(nst),colour(cst){} //构造函数 Fruit(){} }; int main() { Fruit apple(apple); Fruit orange(orange); coutapple orange ?: apple.isSame(orange)endl; //没有问题肯定不同 coutapple /apple/ ?:apple.isSame(string(apple)); //用一个string做形参 return 0; } 除了隐式类类型转换外你还发现什么没有恩就是isSame函数他直接调用了另一个引用的Fruit对象的私有成员name这个按道理是不允许的啊不过注意的是他本身就是Fruit类所以我个人看法纯粹个人看法这里可以认为一个类自动声明为自己类的友元。呵呵不知道对不对。假如你想这样定义 bool Fruit::isSame(const string otherName) { return name otherName; } 然后这样调用 coutapple.isSame(apple2.name)endl;结果是通不过编译的道理还是不能调用一个类的私有成员。最后要说的是我以前以为友元虽然为我们带来了一定的方便但是友元的破坏性也是巨大的他破坏了类的封装不小心使用的话会打击你对C类安全使用的信心就像强制类型转换一样能不用就不用。但是当我看了Bjarne Stroustrup 的书后才理解了一些东西他的意思就是友元是没有人们说的那样的破坏性的因为友元的声明权完全在类设计者手里他能很好控制而不会让友元的特性泛滥而且在我学的更多一些后发现友元在一些应用中必须得用到比如一些操作符的重载不用友元就不行虽然个人感觉类中成员函数省略的This形参假如没有友元作补充支撑根本就不敢用因为会限制很多功能当然有了友元就没有关系了可以在外面定义嘛。友元就讲了这么多不知道是不是复杂化了。