我怎样才能实现这种行为? [英] How can I achieve this behaviour?
问题描述
班级牛
{
公开:
无效EatGrass()const {}
};
class Brow
{
private:
牛牛;
公开:
运营商Cow&()
{
返回牛;
}
};
int main()
{
眉毛k;
k.EatGrass();
}
有什么方法可以实现上面,一个Brow隐含地将b $ b转换成了牛?这是另一个例子:
class Blah
{
private:
双倍;
公开:
运算符double&()
{
返回a;
}
};
int main()
{
Blah jk;
jk = 52.0;
}
-JKop
class Cow
{
public:
void EatGrass() const {}
};
class Brow
{
private:
Cow cow;
public:
operator Cow&()
{
return cow;
}
};
int main()
{
Brow k;
k.EatGrass();
}
Is there any way I can achieve the above, whereby a Brow is implicitly
converted to a Cow? Here would be another example:
class Blah
{
private:
double a;
public:
operator double&()
{
return a;
}
};
int main()
{
Blah jk;
jk = 52.0;
}
-JKop
推荐答案
" JKop" < NU ** @ NULL.NULL>在消息中写道
新闻:W1 ****************** @ news.indigo.ie ...
"JKop" <NU**@NULL.NULL> wrote in message
news:W1******************@news.indigo.ie...
class牛
{
公开:
void EatGrass()const {}
};
class Brow
{
牛牛;
公开:
操作员牛&()
返回牛;
}
};
int main()
{
浏览k;
k.EatGrass();
}
有没有什么方法可以实现上述目的,一个Brow被隐含地转换为一个牛?
class Cow
{
public:
void EatGrass() const {}
};
class Brow
{
private:
Cow cow;
public:
operator Cow&()
{
return cow;
}
};
int main()
{
Brow k;
k.EatGrass();
}
Is there any way I can achieve the above, whereby a Brow is implicitly
converted to a Cow?
是:
class Brow
{
private:
Cow牛;
公众:
Cow&运营商。 ()
{
返回牛;
}
};
:-)
说真的..这是一个理由选择非会员功能成员
功能。
>
class Brow
{
私人:
牛牛;
public:
运营商Cow&()
{
返回牛;
}
运算符const Cow&()const
{
返回牛;
} < br $>
};
无效EatGrass(const Cow&);
Jonathan
Yes:
class Brow
{
private:
Cow cow;
public:
Cow& operator. ()
{
return cow;
}
};
:-)
Seriously .. this is a reasona to prefer non-member functions to member
functions.
class Brow
{
private:
Cow cow;
public:
operator Cow&()
{
return cow;
}
operator const Cow&() const
{
return cow;
}
};
void EatGrass(const Cow&);
Jonathan
Jonathan Turkanis发布:
Jonathan Turkanis posted:
" JKop" < NU ** @ NULL.NULL>在消息中写道
新闻:W1 ****************** @ news.indigo.ie ...
"JKop" <NU**@NULL.NULL> wrote in message
news:W1******************@news.indigo.ie...
class Cow > {
公开:
void EatGrass()const {}};
类眉毛
{
私人:
牛牛;
公开:
操作员牛&()
返回牛; int main()
{
Brow k;
k.EatGrass();
}
有什么方法可以实现上述目的,即Brow会被隐含地转换为Cow?
class Cow
{
public:
void EatGrass() const {} };
class Brow
{
private:
Cow cow;
public:
operator Cow&()
{
return cow; } };
int main()
{
Brow k;
k.EatGrass();
}
Is there any way I can achieve the above, whereby a Brow is implicitly
converted to a Cow?
是的:
班级眉毛
{
私人:
牛牛;
公开:
牛&运营商。 ()
{
返回牛;
}
};
: - )
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;
}
};
模板需要输入所有运算符和变量
参数类型。
-JKop
By 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 variable
argument types.
-JKop
为了基督的缘故,它不会编译。你不能超载。!
-JKop
For christ''s sake it won''t compile. You can''t overload "."!
-JKop
这篇关于我怎样才能实现这种行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!