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
|