Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual function in inherited class not getting called

Reply
Thread Tools

virtual function in inherited class not getting called

 
 
Angus
Guest
Posts: n/a
 
      04-10-2008
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?

 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      04-10-2008
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
}
 
Reply With Quote
 
 
 
 
alasham.said@gmail.com
Guest
Posts: n/a
 
      04-10-2008
On Apr 10, 11:22 am, Angus <(E-Mail Removed)> 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();
}
 
Reply With Quote
 
Angus
Guest
Posts: n/a
 
      04-10-2008
On 10 Apr, 10:34, (E-Mail Removed) wrote:
> On Apr 10, 11:22 am, Angus <(E-Mail Removed)> 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?
 
Reply With Quote
 
alasham.said@gmail.com
Guest
Posts: n/a
 
      04-10-2008
On Apr 10, 11:45 am, Angus <(E-Mail Removed)> wrote:
> On 10 Apr, 10:34, (E-Mail Removed) wrote:
>
>
>
> > On Apr 10, 11:22 am, Angus <(E-Mail Removed)> 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();
}
 
Reply With Quote
 
Krice
Guest
Posts: n/a
 
      04-10-2008
On 10 huhti, 12:45, Angus <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conflict between friend function and inherited class function ciccio C++ 4 01-18-2008 12:13 AM
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM
Destructor not called for inherited class through a template class iTooo C++ 4 11-05-2005 11:02 PM
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM
[Tip] When is Class#inherited called? Gavin Kistner Ruby 0 09-25-2004 05:14 PM



Advertisments