多继承和指针指针 [英] multi-inherit and pointer to pointer
问题描述
这是我的代码
A类;
B类;
C类:公众A,公共B < br $>
{
....
}
D类{
C ** GetC(){
C ** ppC = ...
返回ppC;
} < br $>
}
E级{
无效计算(const B * const * ppB){
...
}
}
int main()
{
D d =新D();
C ** ppC = d.GetC();
E e;
e.Compute(ppC); // ***
}
使用VC ++ 6.0时,// ***的最后一行无法编译。如果我将其更改为
e.COmpute((const B * const *)ppC);
数据未在计算中正确传递;。我该怎么办呢?
谢谢,
John
[见 http://www.gotw.ca/resources/clcm.htm 获取信息关于]
[comp.lang.c ++。主持。第一次海报:做到这一点! ]
Here is my code
Class A;
Class B;
Class C : public A, public B
{
....
}
Class D {
C** GetC(){
C** ppC = ...
return ppC;
}
}
Class E{
void Compute(const B* const* ppB){
...
}
}
int main()
{
D d = new D();
C** ppC = d.GetC();
E e;
e.Compute(ppC); //***
}
With VC++ 6.0 the last line with //*** does not compile. If I change that to
e.COmpute((const B* const*)ppC);
The data is not passed correctly in the "Compute". How can I fix it?
Thanks,
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
ho ****** @ yahoo.com 写道:
这是我的代码
A类;
A类{};
B类;
B级{};
C级:公众A,公共B
C级......
{
...
}
;
D类{
class D {
public:
C ** GetC(){
C ** ppC = ...
返回ppC;
}
}
;
E类{
E级{
public:
void Compute(const B * const * ppB){
...
}
}
int main()
{d / d D =新D();
C ** ppC = d.GetC();
E e;
e.Compute(PPC); // ***
}
使用VC ++ 6.0,// ***的最后一行无法编译。如果我将其更改为
e.COmpute((const B * const *)ppC);
数据未在计算中正确传递。我该如何解决?
Here is my code
Class A;
class A {};
Class B;
class B {};
Class C : public A, public B
class C ...
{
...
} ;
Class D {
class D {
public:
C** GetC(){
C** ppC = ...
return ppC;
}
} ;
Class E{
class E {
public:
void Compute(const B* const* ppB){
...
}
}
int main()
{
D d = new D();
C** ppC = d.GetC();
E e;
e.Compute(ppC); //***
}
With VC++ 6.0 the last line with //*** does not compile. If I change that to
e.COmpute((const B* const*)ppC);
The data is not passed correctly in the "Compute". How can I fix it?
你不能。转化''派生 - > base''仅为指针定义
和引用,但不是指针指针。为什么你需要一个指针
指针呢?你想要完成什么?
Victor
You can''t. Conversions ''derived -> base'' are only defined for pointers
and references, but not pointers to pointers. Why do you need a pointer
to pointer, anyway? What are you trying to accomplish?
Victor
Victor Bazarov发布:
Victor Bazarov posted:
你不能。转化''派生 - > base''仅用于指针
和引用,但不是指针指针。
You can''t. Conversions ''derived -> base'' are only defined for pointers
and references, but not pointers to pointers.
有趣。我不明白为什么不能这样做。
-JKop
Interesting. I don''t see why it can''t be done though.
-JKop
" ; JKop" < NU ** @ NULL.NULL>在消息中写道
新闻:Qd ***************** @ news.indigo.ie ...
"JKop" <NU**@NULL.NULL> wrote in message
news:Qd*****************@news.indigo.ie...
Victor Bazarov发表:
Victor Bazarov posted:
你不能。转化''派生 - > base''仅用于指针
和引用,但不是指针指针。
You can''t. Conversions ''derived -> base'' are only defined for pointers
and references, but not pointers to pointers.
有趣。我不明白为什么不能这样做。
Interesting. I don''t see why it can''t be done though.
因为它会很危险,如果它被允许它会让你对待一个
基础对象作为派生对象。同样的理由不让你把b **兑换成const T **。
struct B {};
struct D:B {void func(); };
B x;
D * p;
B ** q =& p; //从D **转换为B **,这是不允许的
* q =& x; //现在p指向x
p-> func();
从D **转换为B **让我们可以打电话func()on x!
此代码改编自FAQ 18.15
http://www.parashift.com/c++-faq-lit...html#faq-18.15
john
Because it would be dangerous, if it were allowed it would let you treat a
base object as a derived object. It the same reasoning that doesn''t let you
convert T** to const T**.
struct B {};
struct D : B { void func(); };
B x;
D* p;
B** q = &p; // conversion from D** to B**, this is not allowed
*q = &x; // now p is pointing at x
p->func();
Converting from D** to B** has allowed us to call func() on x!
This code is adapted from FAQ 18.15
http://www.parashift.com/c++-faq-lit...html#faq-18.15
john
这篇关于多继承和指针指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!