C++ Classes (problem with events)
-
I'm writing a windows socket wrapper class in c++ from scratch and i cannot get events to work. So inside the class i'll have several events. let's pretend i have the following class:
class SocketTree
{
public:
int Close();virtual void OnClose(); //*this will be the event
}SocketTree::Close()
{
OnClose();
return 0;
}And now i'll have the derived class that will receive the event:
class SckHandle : public SocketTree
{
public:
virtual void OnClose();
}SckHandle::OnClose()
{
printf("working.");
}And here's the main procedure:
int main()
{
SckHandle sHnd;
sHnd.Close();
return 0;
}when sHand.Close() is executed, i wanted the event OnClose() to be fired, but it won't... anyone knows what is happening in here and what should i do to handle events correctly? thank you :)
-
I'm writing a windows socket wrapper class in c++ from scratch and i cannot get events to work. So inside the class i'll have several events. let's pretend i have the following class:
class SocketTree
{
public:
int Close();virtual void OnClose(); //*this will be the event
}SocketTree::Close()
{
OnClose();
return 0;
}And now i'll have the derived class that will receive the event:
class SckHandle : public SocketTree
{
public:
virtual void OnClose();
}SckHandle::OnClose()
{
printf("working.");
}And here's the main procedure:
int main()
{
SckHandle sHnd;
sHnd.Close();
return 0;
}when sHand.Close() is executed, i wanted the event OnClose() to be fired, but it won't... anyone knows what is happening in here and what should i do to handle events correctly? thank you :)
should give a linker error -- SocketTree::OnClose was not defined...if you define it, it will probably work properly. If it is not your intention to have a valid OnClose for SocketTree, then you could declare it as pure virtual by putting a "= 0;" at the end. But then the class is pure virtual, so you can't instantiate objects of that type, only derived objects. Technically speaking, you should also have a "using SocketTree::Close()" as part of your derived class's public area if you intend to use base class functions from your derived objects. Most people probably wouldn't do that I'm guessing, but it is more accurate.
-
I'm writing a windows socket wrapper class in c++ from scratch and i cannot get events to work. So inside the class i'll have several events. let's pretend i have the following class:
class SocketTree
{
public:
int Close();virtual void OnClose(); //*this will be the event
}SocketTree::Close()
{
OnClose();
return 0;
}And now i'll have the derived class that will receive the event:
class SckHandle : public SocketTree
{
public:
virtual void OnClose();
}SckHandle::OnClose()
{
printf("working.");
}And here's the main procedure:
int main()
{
SckHandle sHnd;
sHnd.Close();
return 0;
}when sHand.Close() is executed, i wanted the event OnClose() to be fired, but it won't... anyone knows what is happening in here and what should i do to handle events correctly? thank you :)
-
should give a linker error -- SocketTree::OnClose was not defined...if you define it, it will probably work properly. If it is not your intention to have a valid OnClose for SocketTree, then you could declare it as pure virtual by putting a "= 0;" at the end. But then the class is pure virtual, so you can't instantiate objects of that type, only derived objects. Technically speaking, you should also have a "using SocketTree::Close()" as part of your derived class's public area if you intend to use base class functions from your derived objects. Most people probably wouldn't do that I'm guessing, but it is more accurate.
I fully agree with the above reply. Just one update. The brackets are not allowed with the "using" keyword. using SocketTree::Close; is Ok. Normally this statement is not required.