0

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.

Vijay
  • 2,021
  • 4
  • 24
  • 33
  • friends, this is somewhat urgent. Currently I made it pure virtual but thats not right thing here. I want to allow base class to be instiantiable – Vijay Mar 31 '11 at 10:50
  • This is event like machanism. anyother alternative will also work for me – Vijay Mar 31 '11 at 10:51
  • @Erik : real code is somewhat big and office code can not be shared here. The concept is same. Please understand – Vijay Mar 31 '11 at 10:52
  • 1
    The answer I gave you is correct. If it does not work for your real code, you're doing something wrong - and we can't figure out what's wrong without seeing your code. Look at my sample, test it yourself, then rephrase the question so it demonstrates your *actual* problem. – Erik Mar 31 '11 at 10:54

6 Answers6

6

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

Erik
  • 88,732
  • 13
  • 198
  • 189
  • @Vijay: Show us actual code. Did you remember to make the base class function virtual? – Erik Mar 31 '11 at 10:47
  • @Vijay: In your posted code derived doesn't inherit from base, and that's the only problem *with your posted code*. We can't figure out which problems you have *with your real code* without seeing it. – Erik Mar 31 '11 at 10:59
  • 1
    @Vijay: This solution provided by Erik should work, provided `Accepted` is virtual in Base Class. `Not working` is not a ideal description of the problem. Especially, when someone else is investing his time to solve your problems, Atleast be specific about what is happening. – Alok Save Mar 31 '11 at 11:00
2

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
John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

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.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • IF I declare as pure virtual then I can not create object of base class. In my case i want to allow to create object of base class too. – Vijay Mar 31 '11 at 10:45
0

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

Dhiraj
  • 5
  • 1
0

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

C++ virtual function from constructor

Community
  • 1
  • 1
persiflage
  • 1,154
  • 12
  • 22
-2

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)    
  }  
}
Vijay
  • 2,021
  • 4
  • 24
  • 33
  • 1
    [Please don't use double underscores ('__') in your identifiers](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier); `derived` still does not inherit from `base`; you did not make `Accepted` pure virtual; `__base__` is useless. Why don't you use the (correct) code some of us took the time to write for you instead of writing incorrect code? – Luc Touraille Mar 31 '11 at 12:25