Polymorphism/Inheritance/virtual function problem?

This is a discussion on Polymorphism/Inheritance/virtual function problem? within the C++ Programming forums, part of the General Programming Boards category; Originally posted by JasonLikesJava Without foo() being virtual in class C won't it execute the function in C and not ...

  1. #16
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 07: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-30-2005, 12:21 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 07:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21