A simple question about "virtual" keyword

This is a discussion on A simple question about "virtual" keyword within the C++ Programming forums, part of the General Programming Boards category; Code: class A{ public: virtual void foo(){} }; class B : public A{ public: virtual void foo(){} // void foo(){} ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A simple question about "virtual" keyword

    Code:
    class A{
    public:
      virtual void foo(){}
    };
    
    class B : public A{
    public:
      virtual void foo(){}
      // void foo(){}
    };
    What's the difference between
    Code:
      virtual void foo(){}
    and
    Code:
      void foo(){}
    in a derived class?

    I guess they are the same, am I right? That is to say, we don't need to give a "virtual" keyword for overriding?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    No difference other than style. Whether the virtual keyword is omitted or not from the derived class definition, the foo() is still a virtual function.
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The only difference is that someone looking only at the derived class interface can tell that foo is virtual if the virtual keyword is there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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