Virtual Keyword

This is a discussion on Virtual Keyword within the C++ Programming forums, part of the General Programming Boards category; I have doubt using the keyword "virtual". In C++ if I create a pure virtual function then the derived class ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    41

    Virtual Keyword

    I have doubt using the keyword "virtual". In C++ if I create a pure virtual function then the derived class has to implement it. If the derived class does not declare the pure virtual function with the keyword "virtual" the MVC++ does not warn me about it. But if I had the keyword virtual in the derived class it still doesn't make any difference.

    Here is a sample code:
    Code:
    class A  
    {
    public:
    	 A();
    	 virtual ~A();
    	 virtual void A::op() = 0;
     
    };
     
    
    class B : public A  
    {
    public:
    	 B();
    	 virtual ~B();
    	 void op(); // doesnt make any difference if I have virtual or don't have it.
     
    };
    I want to know if there is any difference using / not using the virtual keyword?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,659
    If the derived class does not declare the pure virtual function with the keyword "virtual" the MVC++ does not warn me about it. But if I had the keyword virtual in the derived class it still doesn't make any difference.
    The virtual function inherited is still virtual either way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I have doubt using the keyword "virtual".
    You have a question. A doubt is different.

    >If the derived class does not declare the pure virtual function
    >with the keyword "virtual" the MVC++ does not warn me about it.
    No compiler will warn you about it. It's perfectly legal to omit the virtual keyword in the derived class, and the member function will still be virtual.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    41
    Thanks for your response, is it used just for better understanding of the code?
    SO IT MAKES NO DIFFERENCE AT ALL IF I USE IT/ NOT RIGHT??

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    [Mis]quoted from a book, if a function is going to be virtual, or you want it to be virtual, just type it and get it over with. You'll remember that way.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> SO IT MAKES NO DIFFERENCE AT ALL IF I USE IT/ NOT RIGHT??

    It makes no difference to the compiler if you don't put it in the derived class, but it does make a difference to people reading your code. Making your code clear and self-explanatory is worth putting a little bit of effort into.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    If the derived class does not declare the pure virtual function with the keyword "virtual" the MVC++ does not warn me about it.
    This is because the compiler assumes that the inherited class will also be an interface (pure virtual class); thus. it doesn't warn you. However, if you tried creating an instance of the inherited class, you would get a compiler warning telling you it's impossible to make an instance of the class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 04:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Stroustrup Talk on C++0x
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-20-2007, 02:02 AM
  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