Thread: Why provide a declaration in the derived class for a virtual base class function

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Why provide a declaration in the derived class for a virtual base class function

    Code:
    #include <iostream>
    using namespace std;
    
    class X
    {
      public:
      X(){}
    
      virtual void f1()
      {
        cout << "In Base" << endl;
      }
      
      void f2()
      {
        cout << "F2 in Base" << endl;
      }
    };
    
    class Y : public X
    {
      public:
      Y(){}
    //  void f1(); If I uncomment this.. it works.. but why does it demand this declaration.
    
    };
    
    void Y::f1()
    {
      cout << "F1 In Derived" << endl;
      // Since f1() is a virtual function in base,
      // why does it demand a declaration in derived too????
    }
    
    int main()
    {
      Y ob;
      ob.f1();
    
      return 0;
    }
    Error Message:
    "test.C", line 29: Error: f1() is not a member of Y.
    1 Error(s) detected.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I found that by not making f1() virtual and commenting out the definition of Y::f1(), that program will compile in Visual Studio 6.0.

    I think that when you declare a virtual function in a base class, it is necessary to declare and define it in the derived class. As for why the declaration is necessary, I think it's simply because the compiler is not a mind reader.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You get an error because, when that line is commented out, your declaration of class Y contains no mention of a f1 function yet you are trying to define one. Yes, the base class already has an f1 function but it doesn't matter, that is the base class f1, not the derived f1 you are trying to create. If you comment that line, you are saying that the derived class does not have an f1 of its own that overrides the virtual f1 defined by the base and so the compiler complains about your attempt at defining one.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM