virtual function in inherited class not getting called

A

Angus

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?
 
B

Barry

Angus said:
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
}
 
A

alasham.said

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();
}
 
A

Angus

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?
 
A

alasham.said

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();
}
 
K

Krice

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top