Thread: Polymorphism/Inheritance/virtual function problem?

  1. #1
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175

    Polymorphism/Inheritance/virtual function problem?

    Code:
    class A{
    public:
    virtual foo() = 0;
    };
    
    class B{};
    
    class C : public A, public B{
    public:
    virtual foo();
    };
    
    class D : public C{
    public:
    foo();
    };
    
    D *d = new D;
    A *a = (A*)d;
    a->foo();  //crashes the program
    What I want the last line to do is call the foo() that is in class D. It compiles but crashes every time. What am I doing wrong?
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Does this run?

    Code:
    #include <iostream>
    
    class A{
    public:
    virtual void foo() = 0;
    };
    
    class B{};
    
    class C : public A, public B{
    public:
    virtual void foo(){std::cout << "C";};
    };
    
    class D : public C{
    public:
    void foo(){std::cout << "D";};
    //Always say if void....include definition
    };
    
    int main(){
    
    D *d = new D;
    if(!d)return 1;
    A *a = static_cast<A*>(d);
    //beter use new casts, but that shouldnt be problem
    a->foo(); 
    a = NULL;
    delete d;
    
    }

  3. #3
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    I can't compile anything on this computer.

    Also, I tried putting std::cout << "whatever" in the foo() functions and it never prints anything, so It doesn't find the functions
    Last edited by JasonLikesJava; 10-21-2002 at 12:15 PM.

  4. #4
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    hmm..works fine here...not using linux nor gcc though..
    maybe it's the casting in conjunction with gcc?

    /btq
    ...viewlexx - julie lexx

  5. #5
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Did you use static_cast<A*> or the way I originally had it?
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I just added that to de tidy,

    But if you were running that code as it was, there were a few ommitions;

    No main function....no explicit return types......no function definitions

    I dont know what compiler you use, or whether there was more code......but the code I posted should work as I see it....

    :: looks again for obvious mistakes ::

  7. #7
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    The real code did have all that other stuff taken care of (return types, main, function definitions )

  8. #8
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    Code:
    #include <iostream.h>
    class A{
    public:
    virtual foo() = 0;
    };
    
    class B{};
    
    class C : public A, public B{
    public:
    	foo(){cout << "B .. " << endl;};
    };
    
    class D : public C{
    public:
    foo(){cout << "D .. " << endl;};
    };
    
    
    
    int main()
    {
    	D *d = new D;
    	A *a = (A*)d;
    	a->foo();  //crashes the program
                    delete d;
    	return 0;
    }
    I just made a simplish app like this and it worked without problems..
    I used msvc++6..and win200000000...

    [edit]Fordy, you code worked like a charm too though [/edit]
    ./btq
    ...viewlexx - julie lexx

  9. #9
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Perhaps I should take another look at my code to make sure this is the right setup I had... I'm pretty sure it was though. I don't have it with me though.
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  10. #10
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    maybe your compiler doesn't like the additional 'virtual' in the class C ?
    (read something about this somewhere)

    /btq
    ...viewlexx - julie lexx

  11. #11
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Another bit of info...

    When I ran it though a debugger and paused the program right before it executed a->foo(), where it showed all the members of class a, it just had vptr.A .... not sure what that means...
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  12. #12
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Without foo() being virtual in class C won't it execute the function in C and not "go through" to D?
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  13. #13
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    on msvc that's __vfptr and I believe it just mean that the function is virtual. It shows some additional information about the virtual function such as memoryadress etc...

    /btq
    ...viewlexx - julie lexx

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by JasonLikesJava
    Another bit of info...

    When I ran it though a debugger and paused the program right before it executed a->foo(), where it showed all the members of class a, it just had vptr.A .... not sure what that means...
    A is an abstract class........that vptr allows you to use the virtual functions.......

    If you have a virtual func, you will most likely have a vptr

  15. #15
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    Without foo() being virtual in class C won't it execute the function in C and not "go through" to D?
    I don't think it should and it doesn't do it here
    I don't see any reason for it to behave as you described, but then again, I might be awfully wrong

    /btq
    ...viewlexx - julie lexx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM