Overriding virtual functions, C# is better than C++
I’ve been merging my UDP framework development branch back to main, building all of my example servers and running all of the “black box” server tests for The Server Framework. In doing so I’ve experienced the pain of changing virtual function signatures. This is one area where C# syntax is definitely better than the C++ syntax…
The Server Framework relies on derivation for extension and use. That wasn’t one of my brightest ideas. As you’ll see from the last free release of the code, creating a TCP/IP server using the framework requires that you derive from the appropriate server base class and override some virtual functions. A simple server might just override OnConnectionEstablished()
and ReadCompleted()
, a more complicated server might filter data before passing it on to the user by using FilterReadCompleted()
and FilterWriteRequest()
, etc.
Due to the changes I needed to make to integrate UDP support into the framework many of these virtual functions now have new signatures:
virtual void ReadCompleted(
JetByteTools::Socket::CAsyncSocket *pSocket,
JetByteTools::IO::IBuffer *pBuffer);
is now:
virtual void ReadCompleted(
JetByteTools::Socket::IStreamSocket *pSocket,
JetByteTools::IO::IBuffer *pBuffer);
This means that I need to chase the signature changes through all of the derived classes (usually just one per server example) and fix up the derived classes to match the base class. It’s not especially hard but it’s one of those things where the compiler can’t help. Since virtual
is optional in the derived classes anyway and the compiler doesn’t distinguish between you overriding a base class function or creating a new virtual function it requires a certain amount of manual work to fix up all of the derived function signatures…
I can’t help thinking that it would be nice to have the same syntax as C#. In C# you need to be a bit more precise about what you’re doing when you override a function. You specify that you’re overriding with the override
keyword in the derived class and the compiler checks that there’s a virtual
function in one of your base classes with the same signature as the override that you’re providing. This functionality would have been handy during my recent changes as the compiler would have alerted me to all of the broken signatures automatically.