Declare Whom Virtual??

This is a discussion on Declare Whom Virtual?? within the C++ Programming forums, part of the General Programming Boards category; Hello, if a simple inheritance is there i am able to know as to which class should i declare as ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Declare Whom Virtual??

    Hello,
    if a simple inheritance is there i am able to know as to which class should i declare as virtual!but when 6,7 classes are involved i get confused. Here is the code which i am posting

    Code:
    #include <iostream.h>
    class A
    {
    	protected:
    	int a;
    };
    class B:public A
    {
    	protected:
    	int b;
    };
    class C:public B
    {
    	protected:
    	int c;
    };
    class D:public C
    {
    	protected:
    	int d;
    };
    class E:public D
    {
    	protected:
    	int e;
    };
    class F:public B,public E
    {
    	protected:
    	int f;
    	public:
    		void putdata()
    		{
    			cout<<a<<endl;
    			cout<<b<<endl;
    			cout<<c<<endl;
    			cout<<d<<endl;
    			cout<<e<<endl;
    			cout<<f<<endl;
    
    		}
    };
    
    int main(void)
    {
    F f;
    
    return 0;
    }
    Here which classes should be declared as virtual ??i mean i know if i put the keyword virtual before B in class C declaration and in class E declaration i get no error. But how?????
    Please state the reason and also explain me how to detect which class should be declared as virtual in complex derivation?

    In the above derivation even A is inherited twice then why have we not declared A as virtual? please provide a detailed yet simple to understand explanation

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    This appears to be a good article on multiple inheritance and how virtual inheritance works:

    http://www.phpcompiler.org/doc/virtualinheritance.html

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    I wish I had never read that .. but good stuff.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Nobody there to reply me????

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by chottachatri View Post

    Here which classes should be declared as virtual ??i mean i know if i put the keyword virtual before B in class C declaration and in class E declaration i get no error. But how?????
    Please state the reason and also explain me how to detect which class should be declared as virtual in complex derivation?

    In the above derivation even A is inherited twice then why have we not declared A as virtual? please provide a detailed yet simple to understand explanation
    I don't get it. None of the classes has any methods (except F), so what would the virtual keyword do?

    And then, what the hell is this "complicated" situation all about. It looks it is way more complicated than necessary. For example, F seems to introduce a "diamond pattern": it is multiply inherited from classes that share the same base class. Avoid such constructs until you are really an expert of C++.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    You're right none of the classes has any methods but just for my reference i want to know..
    Explain me where should be use virtual keyword in the above example and why??

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Uh oh, sorry, I didn't read the link and didn't actually know about virtual inheritance, so your question makes sense indeed.

    However, if you don't want duplicate instantiation of bases, why would you inherit F from B and E. E is already a superclass of B, so why would you introduce the diamond pattern in the first place?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The rule is: if you want to use virtual inheritance to avoid multiple copies of base classes that are appear multiple times in the hierarchy (B in the example above), you must derive virtually exactly from the classes that appear multiple times:
    Code:
    class C : virtual public B
    
    class F : public E, virtual public B
    Don't forget the resulting initialization rules.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also bear in mind that "protected" is essentially the same as public - you only need to create a new class that inherits the original class to freely access the protected member of the function. I'm not saying it's an entirely meaningless property of a class, but it's not very "protecting". In my opinion, member variables should be either public or private. Protected gives you a false sense of security.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by HyperShadow View Post
    I wish I had never read that .. but good stuff.
    I had that feeling when I read it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 10:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

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