简体   繁体   中英

C++ want to call derived function from base class

I have following situation

class base
{
public:

   virtual void Accepted(SOCKET s)  //// Event
   {

   }
   void Listner()
   {
          SOCKET acpted;
          Accepted(acpted); /// When I call I want derived class's Accepted() to get called

   }

};


class derived
{
   virtual void Accepted(SOCKET s)  //// Event
   {
         ////// HERE i will write actual implementation (QUESTION)
   }

}

I want to call derived class's function. This will work like event here. I want to notify derived class that something happened in base class.

class derived : public base will make derived actually inherit from base . Then your virtual function call will work as expected.

Note that you cannot make such virtual function calls in the constructor or destructor of base - At the time base 's constructor is invoked, the derived part doesn't yet exist. At the time base destructor is invoked, the derived part has already been destructed.

EDIT: Demo, in response to comment.

class base
{
public:
    virtual void Accepted(SOCKET s)  //// Event
    {
        cout << "base::Accepted" << endl;
    }
    void Listner()
    {
        SOCKET acpted = 0;
        Accepted(acpted); /// When I call I want derived class's Accepted() to get called
    }
};


class derived : public base
{
    virtual void Accepted(SOCKET s)  //// Event
    {
        cout << "derived::Accepted" << endl;
    }
};


int main(int argc, char * argv[])
{
  derived d;
    d.Listner();
}

This will print derived::Accepted

This works the way you want. If your actual code isnt working, then you havent shown us everything relevant. This is why we implore you to show us real code. Do you suppose you're the only one here that is dealing with a large codebase that cannot be shared on the internet? How do you think the rest of us get help? Write a small example that actually replicates the behavior you're seeing and post that. Don't post psudocode you haven't even compiled.

Here is how you can do what you're trying to do, and also an example of how you should be posting.

#include <iostream>
using namespace std;

class Base
{
public:
    virtual void Accepted()
    {
        cout << "Base::Accepted" << endl;
    }
    void Listener()
    {
        cout << "Base::Listener" << endl;
        Accepted();
    }
};

class Der : public Base
{
public:
    void Accepted()
    {
        cout << "Derived::Accepted" << endl;
    }
};

int main()
{
    cout << "*** BASE ***" << endl;
    Base b;
    b.Listener();

    cout << "\n*** DERIVED ***" << endl;
    Der d;
    d.Listener();
}

Output is:

*** BASE ***
Base::Listener
Base::Accepted

*** DERIVED ***
Base::Listener
Derived::Accepted

In addition to make de derived class extending the base class (derived : public base) you could also declare you Accepted method pure virtual in the base class:

virtual void Accepted(SOCKET s) = 0;

this way the derived class is forced to implement the accepted method.

And also the Accepted method in the derived class doesn't need to be virtual.

Make following changes in your code. 1) class derived : public base 2) Create object of derived class and call Listener function. (If you are creating object of base class, it will not work)

Dhiraj

Be careful not to call your virtual functions during construction or destruction. Bad implementation defined things can happen:

C++ virtual function from constructor

Thanks friend for your answers. All were technically correct. I solved it following way

I wanted to allow creating object of class base by not making any of the method as pure virtual.

If i make any of the method as pure virtual then it makes class base as abstract.

So i create another abstract class in which all required methods are pure virtual and i am deriving class base from that. that way it allowed me to create object of base.

eg

class __base__
{
public:
   virtual void Accepted(SOCKET s)  = 0 //// Event    
   {     
   }    

};

class base : public __base__
{ 
  public:     
       virtual void Accepted(SOCKET s)  //// Event    
        {     
}    
void Listner()    
{           
  SOCKET acpted;           
  Accepted(acpted); /// When I call I want derived class's Accepted() to get called     
}  
};

class derived : public base
{    
  virtual void Accepted(SOCKET s)  //// Event    
  {          
      ////// HERE i will write actual implementation (QUESTION)    
  }  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM