Vijay
Vijay

Reputation: 2067

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.

Upvotes: 0

Views: 1407

Answers (6)

John Dibling
John Dibling

Reputation: 101506

This works the way you want. If your actual code isn't working, then you haven't shown us everything relevant.

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

Upvotes: 2

Vijay
Vijay

Reputation: 2067

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.

e.g.

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)    
  }  
}

Upvotes: -2

Erik
Erik

Reputation: 91320

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

Upvotes: 7

persiflage
persiflage

Reputation: 1204

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

C++ virtual function from constructor

Upvotes: 0

Dhiraj
Dhiraj

Reputation: 5

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

Upvotes: 0

Heisenbug
Heisenbug

Reputation: 39204

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.

Upvotes: 0

Related Questions