Redian新闻
>
请问一个implicit conversion的问题(C++)
avatar
请问一个implicit conversion的问题(C++)# Programming - 葵花宝典
h*o
1
原例抄自于Bjarne的书p 275,让自己定义的类参于已存在的类的运算
代码如H下
avatar
h*o
2
re啊,问题是长了一点。。。

【在 h**o 的大作中提到】
: 原例抄自于Bjarne的书p 275,让自己定义的类参于已存在的类的运算
: 代码如H下

avatar
g*e
3
I read it, but I dont know the answer.:)

【在 h**o 的大作中提到】
: re啊,问题是长了一点。。。
avatar
X*r
4
In case of a function template, template argument deduction is performed
before the compiler tries to resolve the overloading [13.3.1 paragraph 7].
In other words, the compiler sees "a*b", then it looks for all overloaded "
operator*". You have "template operator *(const A&, const A&)
;" defined up there, so it tries to deduct the typename T. Note that the
compiler doesn't look into anything else in this step, including your user-
defined conversion function. Obviously it cannot f
avatar
h*o
5
thx

【在 g***e 的大作中提到】
: I read it, but I dont know the answer.:)
avatar
h*o
6
谢谢,我明白这层意思了
之所以我想用implicit conversion,就是因为class A已经有很多运算了
一个一个再重写既麻烦,我又不知道是不是多余。。。

&)
so
all

【在 X****r 的大作中提到】
: In case of a function template, template argument deduction is performed
: before the compiler tries to resolve the overloading [13.3.1 paragraph 7].
: In other words, the compiler sees "a*b", then it looks for all overloaded "
: operator*". You have "template operator *(const A&, const A&)
: ;" defined up there, so it tries to deduct the typename T. Note that the
: compiler doesn't look into anything else in this step, including your user-
: defined conversion function. Obviously it cannot f

avatar
h*o
7
I get most of your points. But could you belabor a little on your last
sentence please?
The problem I have is that class A is actually built-in in STL and it has
a lot of non-member binary operations. Class B is defined by me. It is a
variant of class A. What I am looking for is a way to implicitly convert
B to A when invoking any function defined for A.
I don't know if this kind of shortcut exits. It is impossible to define a
base class and it looks verbose to define all combinations of A & B f

【在 X****r 的大作中提到】
: In case of a function template, template argument deduction is performed
: before the compiler tries to resolve the overloading [13.3.1 paragraph 7].
: In other words, the compiler sees "a*b", then it looks for all overloaded "
: operator*". You have "template operator *(const A&, const A&)
: ;" defined up there, so it tries to deduct the typename T. Note that the
: compiler doesn't look into anything else in this step, including your user-
: defined conversion function. Obviously it cannot f

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。