virtual class in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepacharya1
    New Member
    • Jun 2010
    • 1

    virtual class in C++

    Consider the case were there are 4 class A,B,C,D

    B and C inherits A
    and D inherits B and C

    why do we define class A as virtual for B and C
    Why not define Class B and C as virtual for Class D ?
    Please answer :)
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A is defined a virtual because it is A that is the class you will end up with 2 copies of if you do not.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Please reply in thread not via PM and particularly do not reply to PMs labelled "Automated PM Please Do Not Reply"

      If B and C did not inherit A virtually then when D inherits B and C it ends up with 2 copies of A, 1 that B refers to and 1 that A refers to. This is normally not good as it leads to ambiguity so B and C inherit A virtually so that when D inherits B and C only 1 copy of A is used which is referred to by both B and C.

      Comment

      Working...