Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Why normal virtual function required blank body in base class

Why normal virtual function required blank body in base class

Scheduled Pinned Locked Moved ATL / WTL / STL
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    am 2009
    wrote on last edited by
    #1

    Hi, I have following block of code,

    class Base
    {
    public:
    Base()
    {
    cout<<"Constructor: Base"<<endl;
    }
    ~Base()
    {
    cout<<"Destructor : Base"<<endl;
    }
    virtual void fun();

    };
    class Derived: public Base
    {

       public:
           Derived()
    	   { 
    		   cout<<"Constructor: Derived"<<endl;
    	   }
           ~Derived()
    	   { 
    		   cout<<"Destructor : Derived"<<endl;
    	   }
    	    void fun()
    	  {
    	    cout<<" in der";
    	  }
    

    };
    int _tmain(int argc, _TCHAR* argv[])
    {
    Derived obj;
    obj.fun();
    getche();

    In above code virtual fun() is showing error that it not resolved. If i add blank body then Derived class's Fun()is called. Is it necessary to have blank body for virtual function in base class and what is reason of this. :omg: Thanks abm

    A 1 Reply Last reply
    0
    • A am 2009

      Hi, I have following block of code,

      class Base
      {
      public:
      Base()
      {
      cout<<"Constructor: Base"<<endl;
      }
      ~Base()
      {
      cout<<"Destructor : Base"<<endl;
      }
      virtual void fun();

      };
      class Derived: public Base
      {

         public:
             Derived()
      	   { 
      		   cout<<"Constructor: Derived"<<endl;
      	   }
             ~Derived()
      	   { 
      		   cout<<"Destructor : Derived"<<endl;
      	   }
      	    void fun()
      	  {
      	    cout<<" in der";
      	  }
      

      };
      int _tmain(int argc, _TCHAR* argv[])
      {
      Derived obj;
      obj.fun();
      getche();

      In above code virtual fun() is showing error that it not resolved. If i add blank body then Derived class's Fun()is called. Is it necessary to have blank body for virtual function in base class and what is reason of this. :omg: Thanks abm

      A Offline
      A Offline
      Alain Rist
      wrote on last edited by
      #2

      Hi, Your Base::fun() member is declared but not defined, so the linker complains. You have to define it somewhere either as pure virtual virtual void fun() = 0; or otherwise:

      //do nothing
      virtual void Base::fun(){};

      cheers, AR

      When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups