-4

Please help me out. I am really not good at polymorphism. I have to classes inherited one from the other.

EDIT: Sorry, fully my mistake. Even I have passed about 2 hours to find the problem. Those who are interested (thanks for all the help)

if (type.compare("TEST1") == 0) result = new Test(ID, database);
if (type.compare("TEST2") == 0) result = new Test(ID, database);
if (type.compare("TOR") == 0) result = new Tor(ID, database);

"==0" was completely missing.

class A {
public:
    void go() {do();}
    virtual void do() {printf('A');}
}

class B:public A {
    virtual void do() {printf('B');}
}

int main {
    A* obj = new B();
    obj->go();
}

The result is 'A' not 'B'. How can I manage to make it 'B'?

Thanks.

Teddy
  • 1,056
  • 1
  • 14
  • 24
  • 1
    **This is not your real code**. And the result **is `B`** if you fix the typos so that code compiles. – Drew Dormann Mar 26 '13 at 18:39
  • 1
    So many errors, makes OP seem lazy. – DanielKO Mar 26 '13 at 18:40
  • No it is not certainly as it would be be long. I just derived the problem I need to solve. Thanks. – Teddy Mar 26 '13 at 18:43
  • 2
    @Teddy, when you say *"The result is 'A' not 'B'"*, that's a lie. You haven't checked what the result of that program is. – Drew Dormann Mar 26 '13 at 18:45
  • Drew: not from this code I admit. – Teddy Mar 26 '13 at 18:51
  • @Teddy I'm glad the mystery was solved. Based on your "Sorry, fully my mistake" edit, I'm voting to close this question. – Drew Dormann Mar 26 '13 at 19:01
  • Thanks Drew. Sometimes one misses obvious things. The real mistery how my code was able to run for weeks! But I found the answer to that one as weel. "TOR" is a new class. Test1 and 2 were existing for weeks. Since they cast the same class the missing "==0" did not cause any trouble until today. – Teddy Mar 26 '13 at 19:04

2 Answers2

5

The code does not compile as is but after several modification, I seem to obtain the desired results:

#include <cstdio>

class A {
public:
    void go() 
    {
       do1();
       };
    virtual void do1() {printf("a");}
};

class B:public A {
    virtual void do1() {printf("b");}
};

int main() {
    A* obj = new B;
    obj->go();
}

First of all do is a reserved word, main was not declared properly and printf takes a char *:

int printf(const char *format, ...);

so you were attempting to do an invalid conversion from char to char *.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
-1

After I tidied some things up to get it to compile:

#include <stdio.h>

class A {
public:
    virtual void DO() {printf("A");}
    void go() {DO();}
};

class B:public A {
    virtual void DO() {printf("B");}
};

int main() {
    A* obj = new B();
    obj->go();
    return 0;
}

(do is a keyword, printf takes a char* not a char, main needs brackets, and returns an integer)

It always produced B for me, regardless of the optimization level I set:

$ g++ inherit.cpp -o inherit && ./inherit
B
$ g++ inherit.cpp -O1 -o inherit && ./inherit
B
$ g++ inherit.cpp -O2 -o inherit && ./inherit
B
$ g++ inherit.cpp -O3 -o inherit && ./inherit
B
$ g++ inherit.cpp -O4 -o inherit && ./inherit
B
$ g++ inherit.cpp -O5 -o inherit && ./inherit
B

However it's possible you've hit an edge case in your compiler, which has decided to inline your call to do() inside go().

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • Thanks. Sorry for the mistakes but I had to derive my code quickly and I am really mad with the issue as the expected behavior is different. Your code does what I need. In deed I should have tried myself. However I do not see the "obvious" difference between your code and my production code. Just 1 question: does it make any difference if you split .h and .cpp files from each other? – Teddy Mar 26 '13 at 18:49
  • There's a rule in C++ specifically for `main` that allows no return statement. It's legal to have no `return` statement and `return 0;` will be assumed. – Drew Dormann Mar 26 '13 at 18:56
  • I don't think it is valid for a compiler to inline a call to a virtual member function in this way: http://www.parashift.com/c++-faq/inline-virtuals.html – Shafik Yaghmour Mar 26 '13 at 18:57
  • 2
    [Optimization levels are up to -O3.](http://stackoverflow.com/questions/1778538/how-many-gcc-optimization-levels-are-there) – milleniumbug Mar 26 '13 at 18:58
  • @DrewDormann maybe I am confused but this seems to imply that it would inline to the correct one: http://stackoverflow.com/questions/6836401/can-a-compiler-inline-a-virtual-function-if-i-use-a-pointer-in-a-clear-situation – Shafik Yaghmour Mar 26 '13 at 19:10
  • @DrewDormann Well then the `potential` explanation for the `A` output can not be correct then, although now my original wording is a bit ambiguous. – Shafik Yaghmour Mar 26 '13 at 19:17
  • @DrewDormann I do see that, what I am saying given that `A` could never have been the output the statement in this answer `However it's possible you've hit an edge case in your compiler, which has decided to inline your call to do() inside go().` can not be correct explanation for an output that never happened. – Shafik Yaghmour Mar 26 '13 at 19:29
  • @ShafikYaghmour I see now. Sorry for the confusion. – Drew Dormann Mar 26 '13 at 19:30