+ Reply to Thread
Results 1 to 2 of 2

Thread: What is vitual base class?

  1. #1

    What is vitual base class?

    Hi All,

    What is vitual base class?

    What are its benefits?

    When should I use it?

    Thanks.

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    There can ba a way to derive a clas from the same base class twice.

    Example:

    Code:
    class CGrandParent
    {
    } ;
    	
    class CParent1 : public CGrandParent
    {
    } ;
    	
    class CParent2 : public CGrandParent
    {
    } ;
    	
    class CChild : public CParent1, public CParent2
    {
    } ;
    In above example, CChild is deriving CGrandParent class for two times!

    One from the CParent1 and another from the CParent2.

    This will lead to two copies of the CGrandParent in CChild class.

    'virtual' base class can be useful to resolve this issue.

    Code:
    class CGrandParent
    {
    } ;
    	
    class CParent1 : virtual public CGrandParent
    {
    } ;
    	
    class CParent2 : virtual public CGrandParent
    {
    } ;
    	
    class CChild : public CParent1, public CParent2
    {
    } ;
    Virtual base classes offer a way to save space and avoid ambiguities in class hierarchies that use multiple inheritance.

    For more information:
    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts