由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 小问题
相关主题
请教各路C++大神 为什么f(3) 输出是 'dd'问行C++代码
问题Python 这种二逼语言怎么设计的
Help: who has gcc 4.0 or higher再问一下
which func will be called?*(&b1)=b编译不过,b1=b可以,区别是?
c++ operator overloading questionquestion regarding const function
请问一个implicit conversion的问题(C++)[合集] question on item15 in Effective C++
怎么搞的?Python里面为什么range(0,5)不用0:5来表示?
could anybody please tell me what " while(cin) {...}" means?Be $#%!ing explicit
相关话题的讨论汇总
话题: operator话题: int话题: print话题: conversion话题: class
进入Programming版参与讨论
1 (共1页)
N***m
发帖数: 4460
1
c++ forbids implicit conversion of user-defined type more than once.
所以试验了一下。
首先是不是只能这样写?能不能用member function之类的?
假定ABC没有继承关系。
这样写起来很麻烦,如果有好多层转换A->B->C...的话。
有没有简化的写法?
======================================================================
#include
using namespace std;
class C {
int i;
public:
C(int j):i(j){}
void Print(){cout<<"C::i="< };
class B {
int i;
public:
B(int j):i(j){}
operator C()
{
return C(3*i);
}
void Print(){cout<<"B::i="< };
cla
z****e
发帖数: 2024
2
你可以不必
operator C()
{
return C(operator B());
}
直接
operator C()
{
return C(6*i);
}
z****e
发帖数: 2024
3
或者给C加几个constructor,takeA,B。
N***m
发帖数: 4460
4
right, in this particular example,
you can always do this straightforwardly.
But I "feel" this is not a somewhat good design, since you need to
know the very details of every conversion. Suppose you change the
implementation of B, then you need to worry about C.
It is easy to make some mistakes here and there.
So are there any smart or simple patterns to follow?

【在 z****e 的大作中提到】
: 你可以不必
: operator C()
: {
: return C(operator B());
: }
: 直接
: operator C()
: {
: return C(6*i);
: }

N***m
发帖数: 4460
5
这个本质上和C(A()),C(B())是差不多的吧?
要是有很多转换,写起来还是麻烦的。
不过,我自己也没想清楚我究竟想说什么:)

【在 z****e 的大作中提到】
: 或者给C加几个constructor,takeA,B。
t****t
发帖数: 6806
6
this is part of class interface design. it's not something with a generic
rule or what you can know in a snap. usually it depends on how your client (
probably yourself) want to use the class.
but most conversion function should be in contructor form, instead of
operator T() form. see EC++.

【在 N***m 的大作中提到】
: 这个本质上和C(A()),C(B())是差不多的吧?
: 要是有很多转换,写起来还是麻烦的。
: 不过,我自己也没想清楚我究竟想说什么:)

N***m
发帖数: 4460
7
I see.thanks for your reply.

(

【在 t****t 的大作中提到】
: this is part of class interface design. it's not something with a generic
: rule or what you can know in a snap. usually it depends on how your client (
: probably yourself) want to use the class.
: but most conversion function should be in contructor form, instead of
: operator T() form. see EC++.

k*******d
发帖数: 1340
8
You mean non-explicit constructor?

generic
client (

【在 t****t 的大作中提到】
: this is part of class interface design. it's not something with a generic
: rule or what you can know in a snap. usually it depends on how your client (
: probably yourself) want to use the class.
: but most conversion function should be in contructor form, instead of
: operator T() form. see EC++.

t****t
发帖数: 6806
9
yes, obviously non-explicit. otherwise you can't do implicit conversion.

【在 k*******d 的大作中提到】
: You mean non-explicit constructor?
:
: generic
: client (

1 (共1页)
进入Programming版参与讨论
相关主题
Be $#%!ing explicitc++ operator overloading question
[合集] 又学了一招请问一个implicit conversion的问题(C++)
感觉学scala不学haskell是不行的怎么搞的?
发现一个有趣的现象。could anybody please tell me what " while(cin) {...}" means?
请教各路C++大神 为什么f(3) 输出是 'dd'问行C++代码
问题Python 这种二逼语言怎么设计的
Help: who has gcc 4.0 or higher再问一下
which func will be called?*(&b1)=b编译不过,b1=b可以,区别是?
相关话题的讨论汇总
话题: operator话题: int话题: print话题: conversion话题: class