Thread: Polymorphism/Inheritance/virtual function problem?

  1. #16
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    Originally posted by JasonLikesJava
    Without foo() being virtual in class C won't it execute the function in C and not "go through" to D?
    No....if its virtual in the base, it stays virtual for the redefinitions....you can label each redefinition as a virtual to remind yourself, but its not compulsary

    :: oops..sorry..btq beat me ::

  2. #17
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Aggghh.... all that for nothing.

    The code giving me problems should have been:

    B*b = new B;
    A *a = (A*)b;
    a->foo(); //crashes the program

    So to fix it I had to first cast into D and then to A

    B*b = new B;
    A *a = (A*)((D*)b);
    a->foo(); //all better

    I noticed it when I used static_cast and gcc gave me a compile error saying it was an invalid cast.

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, 01: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