virtual function in inherited class not getting called

Discussion in 'C++' started by Angus, Apr 10, 2008.

  1. Angus

    Angus Guest

    I have a base class CEventSelect a bit like this:

    class CEventSelect
    {
    virtual void OnConnected();
    }

    and a network interface class a bit like this:

    class networkinterface : public CEventSelect
    {
    virtual void OnConnected();
    }


    The base CEventSelect class calls it's OnConnected() function when a
    client receives notification from the network that it is connected.
    But I wanted my networkinterface::OnConnected() method to be called?
    But it wasn't. I understand polymorphic functions and I think I
    understand why it is not working the way I wanted. Because
    CEventSelect calls its own OnConnected() method does mean that the
    inheriting OnConnected() will be called. But this is the behaviour I
    want.

    The actual connection is notified by the base class - but I want my
    interface class to someone get notified of the connection.

    How can I get the networkinterface class to be notified about the
    connection?
     
    Angus, Apr 10, 2008
    #1
    1. Advertising

  2. Angus

    Barry Guest

    Angus wrote:
    > I have a base class CEventSelect a bit like this:
    >
    > class CEventSelect
    > {
    > virtual void OnConnected();
    > }
    >
    > and a network interface class a bit like this:
    >
    > class networkinterface : public CEventSelect
    > {
    > virtual void OnConnected();
    > }
    >
    >
    > The base CEventSelect class calls it's OnConnected() function when a
    > client receives notification from the network that it is connected.
    > But I wanted my networkinterface::OnConnected() method to be called?
    > But it wasn't. I understand polymorphic functions and I think I
    > understand why it is not working the way I wanted. Because
    > CEventSelect calls its own OnConnected() method does mean that the
    > inheriting OnConnected() will be called. But this is the behaviour I
    > want.
    >
    > The actual connection is notified by the base class - but I want my
    > interface class to someone get notified of the connection.
    >
    > How can I get the networkinterface class to be notified about the
    > connection?
    >


    I don't know how your code was written,
    But I guess the following code may help.

    #include <iostream>

    class CEventSelect
    {
    public:
    virtual void OnConnected() { std::cout << "CEventSelect" <<
    std::endl; }
    };

    class networkinterface : public CEventSelect
    {
    public:
    virtual void OnConnected() { std::cout << "networkinterface" <<
    std::endl; }
    };

    void call1(CEventSelect event)
    {
    event.OnConnected();
    }

    void call2(CEventSelect& event)
    {
    event.OnConnected();
    }

    void call3(CEventSelect* event)
    {
    event->OnConnected();
    }

    int main()
    {
    networkinterface ni;
    call1(ni); // by value
    call2(ni); // by reference
    call3(&ni);// by address
    }
     
    Barry, Apr 10, 2008
    #2
    1. Advertising

  3. Angus

    Guest

    On Apr 10, 11:22 am, Angus <> wrote:
    > I have a base class CEventSelect a bit like this:
    >
    > class CEventSelect
    > {
    > virtual void OnConnected();
    >
    > }
    >
    > and a network interface class a bit like this:
    >
    > class networkinterface : public CEventSelect
    > {
    > virtual void OnConnected();
    >
    > }
    >
    > The base CEventSelect class calls it's OnConnected() function when a
    > client receives notification from the network that it is connected.
    > But I wanted my networkinterface::OnConnected() method to be called?
    > But it wasn't. I understand polymorphic functions and I think I
    > understand why it is not working the way I wanted. Because
    > CEventSelect calls its own OnConnected() method does mean that the
    > inheriting OnConnected() will be called. But this is the behaviour I
    > want.
    >
    > The actual connection is notified by the base class - but I want my
    > interface class to someone get notified of the connection.
    >
    > How can I get the networkinterface class to be notified about the
    > connection?


    Polymorphic function dispatch mechanism requires the use of pointers
    or references. The following should work.
    Regards.

    #include<iostream>

    class CEventSelect
    {
    public:
    virtual void OnConnected() { std::cout <<
    "CEventSelect::OnConnected()\n"; }
    };

    class networkinterface : public CEventSelect
    {
    public:
    virtual void OnConnected() { std::cout <<
    "networkinterface::OnConnected()\n"; }
    };

    int main()
    {
    CEventSelect* p = new networkinterface();
    p->OnConnected();
    delete p;

    std::cin.get();
    }
     
    , Apr 10, 2008
    #3
  4. Angus

    Angus Guest

    On 10 Apr, 10:34, wrote:
    > On Apr 10, 11:22 am, Angus <> wrote:
    >
    >
    >
    >
    >
    > > I have a base class CEventSelect a bit like this:

    >
    > > class CEventSelect
    > > {
    > >  virtual void OnConnected();

    >
    > > }

    >
    > > and a network interface class a bit like this:

    >
    > > class networkinterface : public CEventSelect
    > > {
    > >         virtual void OnConnected();

    >
    > > }

    >
    > > The base CEventSelect class calls it's OnConnected() function when a
    > > client receives notification from the network that it is connected.
    > > But I wanted my networkinterface::OnConnected() method to be called?
    > > But it wasn't.  I understand polymorphic functions and I think I
    > > understand why it is not working the way I wanted.  Because
    > > CEventSelect calls its own OnConnected() method does mean that the
    > > inheriting OnConnected() will be called.  But this is the behaviour I
    > > want.

    >
    > > The actual connection is notified by the base class - but I want my
    > > interface class to someone get notified of the connection.

    >
    > > How can I get the networkinterface class to be notified about the
    > > connection?

    >
    > Polymorphic function dispatch mechanism requires the use of pointers
    > or references. The following should work.
    > Regards.
    >
    > #include<iostream>
    >
    > class CEventSelect
    > {
    > public:
    >     virtual void OnConnected() { std::cout <<
    > "CEventSelect::OnConnected()\n"; }
    >
    > };
    >
    > class networkinterface : public CEventSelect
    > {
    > public:
    >     virtual void OnConnected() { std::cout <<
    > "networkinterface::OnConnected()\n"; }
    >
    > };
    >
    > int main()
    > {
    >     CEventSelect* p = new networkinterface();
    >     p->OnConnected();
    >     delete p;
    >
    >     std::cin.get();
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    You misunderstand my question. I am not calling OnConnected from
    networkinterface. OnConnected() is called from an event inside
    CEventSelect. That is my problem. But I want the networkinterface
    class to be notified of the OnConnected 'event' happening.

    So my notification event is happening in my base class. Of course the
    base class knows nothing of networkinterface - hence OnConnected() in
    networkinterface is not called.

    Maybe virtual functions are not the right answer here. Maybe I need
    to notify some other way?
     
    Angus, Apr 10, 2008
    #4
  5. Angus

    Guest

    On Apr 10, 11:45 am, Angus <> wrote:
    > On 10 Apr, 10:34, wrote:
    >
    >
    >
    > > On Apr 10, 11:22 am, Angus <> wrote:

    >
    > > > I have a base class CEventSelect a bit like this:

    >
    > > > class CEventSelect
    > > > {
    > > > virtual void OnConnected();

    >
    > > > }

    >
    > > > and a network interface class a bit like this:

    >
    > > > class networkinterface : public CEventSelect
    > > > {
    > > > virtual void OnConnected();

    >
    > > > }

    >
    > > > The base CEventSelect class calls it's OnConnected() function when a
    > > > client receives notification from the network that it is connected.
    > > > But I wanted my networkinterface::OnConnected() method to be called?
    > > > But it wasn't. I understand polymorphic functions and I think I
    > > > understand why it is not working the way I wanted. Because
    > > > CEventSelect calls its own OnConnected() method does mean that the
    > > > inheriting OnConnected() will be called. But this is the behaviour I
    > > > want.

    >
    > > > The actual connection is notified by the base class - but I want my
    > > > interface class to someone get notified of the connection.

    >
    > > > How can I get the networkinterface class to be notified about the
    > > > connection?

    >
    > > Polymorphic function dispatch mechanism requires the use of pointers
    > > or references. The following should work.
    > > Regards.

    >
    > > #include<iostream>

    >
    > > class CEventSelect
    > > {
    > > public:
    > > virtual void OnConnected() { std::cout <<
    > > "CEventSelect::OnConnected()\n"; }

    >
    > > };

    >
    > > class networkinterface : public CEventSelect
    > > {
    > > public:
    > > virtual void OnConnected() { std::cout <<
    > > "networkinterface::OnConnected()\n"; }

    >
    > > };

    >
    > > int main()
    > > {
    > > CEventSelect* p = new networkinterface();
    > > p->OnConnected();
    > > delete p;

    >
    > > std::cin.get();

    >
    > > }- Hide quoted text -

    >
    > > - Show quoted text -- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > You misunderstand my question. I am not calling OnConnected from
    > networkinterface. OnConnected() is called from an event inside
    > CEventSelect. That is my problem. But I want the networkinterface
    > class to be notified of the OnConnected 'event' happening.
    >
    > So my notification event is happening in my base class. Of course the
    > base class knows nothing of networkinterface - hence OnConnected() in
    > networkinterface is not called.
    >
    > Maybe virtual functions are not the right answer here. Maybe I need
    > to notify some other way?



    The point is that you can use a pointer or reference to
    networkinterface object wherever CEventSelect pointer or reference is
    being used, but I do not know if you have control over that (for
    example, if you are using some third party library). Here is an
    example. CEventSelect::Event calls the virtual OnConnected. Hope that
    may help.

    Regards.

    #include<iostream>

    class CEventSelect
    {
    public:
    virtual void OnConnected() { std::cout <<
    "CEventSelect::OnConnected()\n"; }

    void Event() { OnConnected(); }
    };

    class networkinterface : public CEventSelect
    {
    public:
    virtual void OnConnected() { std::cout <<
    "networkinterface::OnConnected()\n"; }

    };

    int main()
    {
    CEventSelect* p = new networkinterface();
    p->Event();
    delete p;

    std::cin.get();
    }
     
    , Apr 10, 2008
    #5
  6. Angus

    Krice Guest

    On 10 huhti, 12:45, Angus <> wrote:
    > You misunderstand my question. I am not calling OnConnected from
    > networkinterface. OnConnected() is called from an event inside
    > CEventSelect. That is my problem.


    Hey, I have an idea. Don't use virtual function at all. Call
    the base class function from the derived class, after all
    everything that is in base class should be a part of derived
    class too.
     
    Krice, Apr 10, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    727
    Richard Herring
    Oct 18, 2005
  2. iTooo
    Replies:
    4
    Views:
    520
    Peter_Julian
    Nov 5, 2005
  3. Mark Sullivan
    Replies:
    0
    Views:
    273
    Mark Sullivan
    Dec 21, 2007
  4. Gavin Kistner

    [Tip] When is Class#inherited called?

    Gavin Kistner, Sep 25, 2004, in forum: Ruby
    Replies:
    0
    Views:
    116
    Gavin Kistner
    Sep 25, 2004
  5. 7stud --
    Replies:
    11
    Views:
    416
    7stud --
    Nov 9, 2007
Loading...

Share This Page