Thread: Virtual Keyword

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,897
    >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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    [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,366
    >> 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, 05: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, 11: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