class Brow { 私人: 牛牛; public: 运营商Cow&() { 返回牛; } 运算符const Cow&()const { 返回牛; } }; 无效EatGrass(const Cow&); JonathanYes:class Brow{private:Cow cow;public:Cow& operator. (){return cow;}};:-)Seriously .. this is a reasona to prefer non-member functions to memberfunctions.class Brow{private:Cow cow;public:operator Cow&(){return cow;}operator const Cow&() const{return cow;}};void EatGrass(const Cow&);Jonathan 是的: 班级眉毛 {私人: 牛牛; 公开: 牛&运营商。 () {返回牛; } }; : - ) Yes: class Brow { private: Cow cow; public: Cow& operator. () { return cow; } };:-) 我很喜欢它! 现在我们可以做到以下几点: void SomeFunc(Cow& k); int main() { Brow t; SomeFunc(t) ; //运营商Cow& } 另外我们可以访问它的成员: int main () { Brow t; t.EatGrass(); } 唯一剩下的就是访问它的运营商: int main() { Brow t; t = 6; //假设Cow有一个赋值运算符 } 有没有简单的方法可以做到这一点,除了押韵之外: class Brow { 私人: 牛f; 公开: operator ==(const Cow& r) { return f == r; } 运算符*(const Cow& r) { 返回f * r; } }; 模板需要输入所有运算符和变量 参数类型。 -JKopBy golly I love it!So now we can do the following:void SomeFunc(Cow& k);int main(){Brow t;SomeFunc(t); //operator Cow&}Plus we can access it''s members:int main(){Brow t;t.EatGrass();}The only thing left is to access its operators:int main(){Brow t;t = 6;//Assuming Cow has an assignment operator}Is there any easy way to do this, other than rhyming them all off:class Brow{private:Cow f;public:operator==(const Cow& r){return f == r;}operator*(const Cow& r){return f * r;}};Templates would be required to get in all the operators and variableargument types.-JKop 这篇关于我怎样才能实现这种行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 01:12
查看更多