char text_c [100]; C(){* text_c = 0; } ~C(){} }; // --------------- -------------------------------------------------- -------------- 无效测试(){ B * bp1,* bp2; C c,* cp1,* cp2,* cp3; void * p; strcpy(c.text_a," hello a" ;); strcpy(c.text_b," hello b"); strcpy(c.text_c," hello c"); cp1 =& c; p = cp1; bp1 = cp1; // ok bp2 =(B *)p; //结果bp2错了! cp2 =(C *)p; // ok cp3 =(C *)bp2; //结果cp3错了!这是合乎逻辑的,因为bp2 已经错了。 } // -------------- -------------------------------------------------- ------------- 所以热点是bp2 =(B *)p; 这有什么问题??? 我可以想象有人说'p没有指向B对象'。 但是如果你想一想,从概念上来说,确实如此,imho。 所以编译这个更加技术性了!? 也许我'愚蠢和/或缺少关于C ++必不可少的东西。 如果是这样,请给我一个指向我可以学习这个权利的链接。 干杯, Joclass A {public:char text_a[100];A() { *text_a=0; }~A() {}};//-----------------------------------------------------------------------------class B {public:char text_b[100];B() { *text_b=0; }~B() {}};//-----------------------------------------------------------------------------class C : public A, public B {public:char text_c[100];C() { *text_c=0; }~C() {}};//-------------------------------------------------------------------------------void test() {B *bp1,*bp2;C c,*cp1,*cp2,*cp3;void *p;strcpy(c.text_a,"hello a");strcpy(c.text_b,"hello b");strcpy(c.text_c,"hello c");cp1=&c;p=cp1;bp1=cp1; // okbp2=(B*)p; // resulting bp2 is WRONG!cp2=(C*)p; // okcp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2is already wrong.}//-----------------------------------------------------------------------------So the hot spot is the bp2=(B*)p;What''s wrong with that???I can imagine someone saying "p is not pointing to a B object".But if you think about it, conceptually, it does, imho.So is it more a technical matter of compiling this!?Maybe i''m stupid and/or missing something essential about C++.If so, please give me a link to where i can study this right.Cheers,Jo推荐答案 6月18日,11:17 am,Jo< jo.lan ... @ telenet.bewrote:On Jun 18, 11:17 am, Jo <[email protected]: A级{ public: char text_a [100]; A(){* text_a = 0; } ~A(){}}; // ------------------- -------------------------------------------------- -------- B级{ 公开: char text_b [100] ; B(){* text_b = 0; } ~B(){}}; // ------------------- -------------------------------------------------- -------- C级:公众A,公共B { 公开: char text_c [100]; C(){* text_c = 0; } ~C(){}}; // ------------------- -------------------------------------------------- ---------- 无效测试(){ B * bp1,* bp2; C c,* cp1,* cp2,* cp3; void * p; strcpy(c.text_a," hello a"); strcpy(c.text_b," hello b"); strcpy(c.text_c," hello c"); cp1 =& c; p = cp1; bp1 = cp1; // ok bp2 =(B *)p; //结果bp2错了! cp2 =(C *)p; // ok cp3 =(C *)bp2; //结果cp3错了!这是合乎逻辑的,因为bp2 已经错了。} // ------------------ -------------------------------------------------- --------- 所以热点是bp2 =(B *)p; 什么''那个错了??? 我可以想象有人说'p没有指向B对象'。 但是如果你想一想,从概念上讲,它确实如此。 所以编译这个更加技术性了!? 也许我是傻瓜和/或缺少关于C ++的必要条件。 如果是这样,请给我一个指向我可以正确学习的地方的链接。 干杯, Joclass A { public: char text_a[100]; A() { *text_a=0; } ~A() {}};//-----------------------------------------------------------------------------class B { public: char text_b[100]; B() { *text_b=0; } ~B() {}};//-----------------------------------------------------------------------------class C : public A, public B { public: char text_c[100]; C() { *text_c=0; } ~C() {}};//-------------------------------------------------------------------------------void test() {B *bp1,*bp2;C c,*cp1,*cp2,*cp3;void *p; strcpy(c.text_a,"hello a"); strcpy(c.text_b,"hello b"); strcpy(c.text_c,"hello c"); cp1=&c; p=cp1; bp1=cp1; // ok bp2=(B*)p; // resulting bp2 is WRONG! cp2=(C*)p; // ok cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2is already wrong.}//-----------------------------------------------------------------------------So the hot spot is the bp2=(B*)p;What''s wrong with that???I can imagine someone saying "p is not pointing to a B object".But if you think about it, conceptually, it does, imho.So is it more a technical matter of compiling this!?Maybe i''m stupid and/or missing something essential about C++.If so, please give me a link to where i can study this right.Cheers,Jo 我重新编写了你的测试函数 - int main (){b / b b * bp1,* bp2; C c,* cp1; void * p; int a; strcpy(c.text_a," hello a"); strcpy(c.text_b,"你好b"); strcpy(c.text_c," hello c"); cp1 =& c; p = cp1; bp2 = (B *)p; std :: cout<< p<<" \ n"; std :: cout<< bp2; std :: cin>> a; } 我在p和bp2中打印了值。两者都是一样的。 在我的m / c上,O / P是 - 0x22fe28 0x22fe28 为什么你认为在你的程序中导致bp2错误?I re-wrote your test function like this -int main() {B *bp1,*bp2;C c,*cp1;void *p;int a;strcpy(c.text_a,"hello a");strcpy(c.text_b,"hello b");strcpy(c.text_c,"hello c");cp1=&c;p=cp1;bp2=(B*)p;std::cout<<p<<"\n";std::cout<<bp2;std::cin>>a;}I printed the value in p and also in bp2. Both are one and the same.On my m/c the O/P was -0x22fe280x22fe28Why do you think, in your program, resulting bp2 is WRONG? Bharath写道:Bharath wrote: > 6月18日上午11点17分,Jo< jo.lan ... @ telenet.bewrote:>On Jun 18, 11:17 am, Jo <[email protected]: >> A级{ 公开: char text_a [100]; A(){* text_a = 0 ; } ~A(){}}; // --------------------------- -------------------------------------------------- B班{ 公共: [100]; B(){* text_b = 0; } ~B(){}}; // --------------------------- -------------------------------------------------- C班:公众A,公共B { 公共: char text_c [100]; C(){* text_c = 0; } ~C(){}}; // --------------------------- -------------------------------------------------- - void test(){ B * bp1,* bp2; C c,* cp1,* cp2,* cp3; void * p; strcpy(c.text_a," hello a"); strcpy(c.text_b," hello b"); strcpy(c.text_c,"你好c"); cp1 =& c; p = cp1; bp1 = cp1; // ok bp2 =(B *)p; //结果bp2错了! cp2 =(C *)p; // ok cp3 =(C *)bp2; //结果cp3错了!这是合乎逻辑的,因为bp2 已经错了。} // -------------------------- -------------------------------------------------- - 所以热点是bp2 =(B *)p; 那有什么问题??? 我可以想象有人说p没有指向B对象。但是如果你想一想,从概念上来说,确实如此。我不知道。 所以它更像是一个技术问题编译这个!? 也许我很愚蠢和/或缺少一些关于C ++的重要信息。 如果是这样,请给我一个链接,我可以在哪里学习这个权利。 干杯,>>class A { public: char text_a[100]; A() { *text_a=0; } ~A() {}};//-----------------------------------------------------------------------------class B { public: char text_b[100]; B() { *text_b=0; } ~B() {}};//-----------------------------------------------------------------------------class C : public A, public B { public: char text_c[100]; C() { *text_c=0; } ~C() {}};//-------------------------------------------------------------------------------void test() {B *bp1,*bp2;C c,*cp1,*cp2,*cp3;void *p; strcpy(c.text_a,"hello a"); strcpy(c.text_b,"hello b"); strcpy(c.text_c,"hello c"); cp1=&c; p=cp1; bp1=cp1; // ok bp2=(B*)p; // resulting bp2 is WRONG! cp2=(C*)p; // ok cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2is already wrong.}//-----------------------------------------------------------------------------So the hot spot is the bp2=(B*)p;What''s wrong with that???I can imagine someone saying "p is not pointing to a B object".But if you think about it, conceptually, it does, imho.So is it more a technical matter of compiling this!?Maybe i''m stupid and/or missing something essential about C++.If so, please give me a link to where i can study this right.Cheers,Jo 我重写了你的测试功能 - int main(){ B * bp1,* bp2; C c,* cp1; void * p; int a; strcpy(c.text_a," hello a"); strcpy(c.text_b," hello b"); strcpy (c.text_c,hello c); cp1 =& c; p = cp1; bp2 =(B *)p; std :: cout<< p<<" \ n" ;; std :: cout<< bp2; std :: cin>> a; } 我在p和bp2中打印了值。两者都是同一个。在我的m / c上,O / P是 - 0x22fe28 0x22fe28 为什么你认为,在你的程序中,结果bp2错了? I re-wrote your test function like this -int main() {B *bp1,*bp2;C c,*cp1;void *p;int a; strcpy(c.text_a,"hello a"); strcpy(c.text_b,"hello b"); strcpy(c.text_c,"hello c"); cp1=&c; p=cp1; bp2=(B*)p; std::cout<<p<<"\n"; std::cout<<bp2; std::cin>>a;}I printed the value in p and also in bp2. Both are one and the same.On my m/c the O/P was -0x22fe280x22fe28Why do you think, in your program, resulting bp2 is WRONG? 因为我遇到了运行时问题。 所以我将所有内容缩小到上面的代码。 /> 在我的电脑上运行它会产生错误的bp2指针。我可以在 " Watch"中找到它。小组。 我也做了类似的事情:在测试结束时/主要 功能我做了一个 OutputDebugString(cp3-> text_a); OutputDebugString(cp3-> text_b); OutputDebugString(cp3-> text_c); 这为文本提供了垃圾!!! 使用Visual C ++编译7.1.3088Because i got runtime problems.So i narrowed down everything to the above code.Running this on my PC gives a wrong bp2 pointer. I can see it in the"Watch" panel.And i also did a similar thing than you: at the end of the test/mainfunction i did aOutputDebugString(cp3->text_a);OutputDebugString(cp3->text_b);OutputDebugString(cp3->text_c);This gives rubbish for text a!!!Compilation is done with Visual C++ 7.1.3088 Jo写道:Jo wrote: A级{ public: char text_a [100]; A(){* text_a = 0; } ~A(){} }; // --------------- -------------------------------------------------- ------------ B级{ 公开: char text_b [100]; B(){* text_b = 0; } ~B(){} }; // --------------- -------------------------------------------------- ------------ class C:public A,public B { public: char text_c [100]; C(){* text_c = 0; } ~C(){} }; // --------------- -------------------------------------------------- -------------- void test(){ B * bp1,* bp2; C c,* cp1,* cp2,* cp3; void * p; strcpy(c.text_a, hello a); strcpy(c.text_b," hello b"); strcpy(c.text_c," hello c"); cp1 =& c; p = cp1; bp1 = cp1; // ok bp2 =(B *)p; //结果bp2错了! cp2 =(C *)p; // ok cp3 =(C *)bp2; //结果cp3错了!这是合乎逻辑的,因为bp2已经错了。 } // -------------- -------------------------------------------------- ------------- 所以热点是bp2 =(B *)p; 这有什么问题??? 我可以想象有人说'p没有指向B对象'。class A { public: char text_a[100]; A() { *text_a=0; } ~A() {}};//-----------------------------------------------------------------------------class B { public: char text_b[100]; B() { *text_b=0; } ~B() {}};//-----------------------------------------------------------------------------class C : public A, public B { public: char text_c[100]; C() { *text_c=0; } ~C() {}};//-------------------------------------------------------------------------------void test() {B *bp1,*bp2;C c,*cp1,*cp2,*cp3;void *p; strcpy(c.text_a,"hello a"); strcpy(c.text_b,"hello b"); strcpy(c.text_c,"hello c"); cp1=&c; p=cp1; bp1=cp1; // ok bp2=(B*)p; // resulting bp2 is WRONG! cp2=(C*)p; // ok cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2 isalready wrong.}//-----------------------------------------------------------------------------So the hot spot is the bp2=(B*)p;What''s wrong with that???I can imagine someone saying "p is not pointing to a B object". 正确。Right. 但是如果你想一想,从概念上讲,它确实如此。But if you think about it, conceptually, it does, imho. 不,它没有。No, it doesn''t. > 那么编译这个就更具技术性了!?>So is it more a technical matter of compiling this!? 没有代码错误。No the code is wrong. > 也许我愚蠢和/或缺少关于C ++的重要信息。>Maybe i''m stupid and/or missing something essential about C++. 我怀疑你是傻瓜但是你肯定错过了必不可少的东西。I doubt you''re stupid but you''re certainly missing something essential. > ; 如果是这样,请给我一个链接,我可以在哪里学习这个权利。>If so, please give me a link to where i can study this right. 我不知道为什么你认为代码应该是正确的,这是我的问题。 也许你在期待一个普通的演员像dynamic_cast一样工作? 写的时候 bp2 =(B *)p; //结果bp2错了! 你期望程序''发现'那个B对象隐藏了 对象里面是p指向? 如果是google for dynamic_cast,或者更好的还是读一本好书。 johnI have no idea why you think that code should be right, that is my problem.Perhaps you are expecting an ordinary cast to work like a dynamic_cast?When you writebp2=(B*)p; // resulting bp2 is WRONG!you are expecting the program to ''discover'' that there a B object hiddeninside the object that p is pointing to?If so google for dynamic_cast, or better still read a good book.john 这篇关于我不明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-15 20:27