由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问题
相关主题
小问题C++ Q05: pointer to constant variable
请教 C++的一个困惑 (operator delete)大侠进来看看这个问题
which func will be called?C++ Q93 - Q95 (转载)
菜鸟请教smart pointerC++ implicit typename的问题
请教各路C++大神 为什么f(3) 输出是 'dd'问两个C++语法问题
C++ formatted output questiona simple C++ question
question regarding const functiona question about const reference
[合集] question about a static pointer in a member function关于数组
相关话题的讨论汇总
话题: const话题: d2话题: operator话题: say话题: public
进入Programming版参与讨论
1 (共1页)
N***m
发帖数: 4460
1
自己随便写的。但是不明白为什么say((const B*)&d2)不显式调用
operator const B*() in D2。难道slicing的优先级高抑或其他?
我概念不太清楚,望指教!
==========================================================
#include
using namespace std;
class B {
public:
virtual void say() const =0;
};
class D1 :public B {
string s;
public:
virtual void say() const
{
cout<<"bark!"< }
};
class D2 :public B {
string s;
public:
virtual void say() const
{
cout<<"bark! bark!"< }
operator const B*()
{
const B *
t****t
发帖数: 6806
2
to call D2::operator const B*(), you have to write
(const B*)d2

【在 N***m 的大作中提到】
: 自己随便写的。但是不明白为什么say((const B*)&d2)不显式调用
: operator const B*() in D2。难道slicing的优先级高抑或其他?
: 我概念不太清楚,望指教!
: ==========================================================
: #include
: using namespace std;
: class B {
: public:
: virtual void say() const =0;
: };

d****p
发帖数: 685
3
Your operator tries to convert D2, instead of D2*, to B*.
And in C++ upcast pointer of child class to pointer of parent class, is
implicit and automatic.

【在 N***m 的大作中提到】
: 自己随便写的。但是不明白为什么say((const B*)&d2)不显式调用
: operator const B*() in D2。难道slicing的优先级高抑或其他?
: 我概念不太清楚,望指教!
: ==========================================================
: #include
: using namespace std;
: class B {
: public:
: virtual void say() const =0;
: };

N***m
发帖数: 4460
4
大虾,我没太看懂你说的意思:)
你是说 say((const B*)d2)?
编译不通过阿。

【在 t****t 的大作中提到】
: to call D2::operator const B*(), you have to write
: (const B*)d2

N***m
发帖数: 4460
5
so you mean upcasting is always of first priority?
So there is no ambiguity for upcasting in any case?
Is there any way to customize this behavior? say,
output sth when upcasting?

【在 d****p 的大作中提到】
: Your operator tries to convert D2, instead of D2*, to B*.
: And in C++ upcast pointer of child class to pointer of parent class, is
: implicit and automatic.

t****t
发帖数: 6806
6
of course, because you declared "const D2 d2", while the cast operator is "
operator const B*()" which is mismatch. if you used "D2 d2", or "operator
const B*() const" you can pass. can't you read the error message?

【在 N***m 的大作中提到】
: 大虾,我没太看懂你说的意思:)
: 你是说 say((const B*)d2)?
: 编译不通过阿。

N***m
发帖数: 4460
7
error message 不是很 helpful阿,特别是对新手。
我改了一下,现在工作了,我也理解了。多谢!

【在 t****t 的大作中提到】
: of course, because you declared "const D2 d2", while the cast operator is "
: operator const B*()" which is mismatch. if you used "D2 d2", or "operator
: const B*() const" you can pass. can't you read the error message?

d****p
发帖数: 685
8
It is not the first priority - it is the only thing that will happen.
And it happens at compile time so there is no way to inject your handler.

【在 N***m 的大作中提到】
: so you mean upcasting is always of first priority?
: So there is no ambiguity for upcasting in any case?
: Is there any way to customize this behavior? say,
: output sth when upcasting?

N***m
发帖数: 4460
9
got your point le:)
3x~~~

【在 d****p 的大作中提到】
: It is not the first priority - it is the only thing that will happen.
: And it happens at compile time so there is no way to inject your handler.

z****e
发帖数: 2024
10
1. change your type conversion to a const mem fun
operator const B* () const;
2. call the following with no conversion.
say(d2);

【在 N***m 的大作中提到】
: 大虾,我没太看懂你说的意思:)
: 你是说 say((const B*)d2)?
: 编译不通过阿。

N***m
发帖数: 4460
11
3x.I like 2 very much!
It's much simpler.
Why did I always come across bad designs! what a sigh!

【在 z****e 的大作中提到】
: 1. change your type conversion to a const mem fun
: operator const B* () const;
: 2. call the following with no conversion.
: say(d2);

k*******d
发帖数: 1340
12
In your original code, say((const B*)&d2);, removing (const B*) does not
make any difference because pointer to derived ==> pointer to base is
implicit and very commonly used in C++.
The discussion here let me understand more about the type conversion
operator, thanks, guys. The feature is relatively rarely used in C++,
compare to the one above.
But lz's design itself does not make much sense. Why do you want to convert
an object to a pointer to its base class...I don't think people do this in
p
N***m
发帖数: 4460
13
sorry for the confusion.
It is not meant to be a practical/useful code.
Just a test to see if I understand the basic concept.
Obviously, I failed:)

convert

【在 k*******d 的大作中提到】
: In your original code, say((const B*)&d2);, removing (const B*) does not
: make any difference because pointer to derived ==> pointer to base is
: implicit and very commonly used in C++.
: The discussion here let me understand more about the type conversion
: operator, thanks, guys. The feature is relatively rarely used in C++,
: compare to the one above.
: But lz's design itself does not make much sense. Why do you want to convert
: an object to a pointer to its base class...I don't think people do this in
: p

1 (共1页)
进入Programming版参与讨论
相关主题
关于数组请教各路C++大神 为什么f(3) 输出是 'dd'
请教一个const pointer的问题C++ formatted output question
C++里get array size的问题 (转载)question regarding const function
C++ Function Pointer Array 的问题[合集] question about a static pointer in a member function
小问题C++ Q05: pointer to constant variable
请教 C++的一个困惑 (operator delete)大侠进来看看这个问题
which func will be called?C++ Q93 - Q95 (转载)
菜鸟请教smart pointerC++ implicit typename的问题
相关话题的讨论汇总
话题: const话题: d2话题: operator话题: say话题: public