Overriding virtual functions, C# is better than C++

| 2 Comments
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.

2 Comments

Hi Len,

IIRC, they're letting that precise feature leak from C++/CLI into native C++, for good and bad.

I think it's optional, so it doesn't catch all the cases C# would, but some of it is there.

http://msdn2.microsoft.com/en-us/library/z8ew2153

Then again, there's the problem of bifurcation between what is standard C++ and what's Microsoft C++. I don't mind much, but portability enthusiasts may.

- Kim

Thanks for the link Kim. It looks like quite a useful MS extension. I like the idea of "context sensitive keywords" (which allows the introduction of some new keywords without making it a breaking change due the keywords being used as an identifier somewhere. I guess that also explains the slightly strange location of the override keyword...

It also looks like something that can just be pushed into a macro so that it can be conditionally defined for compilers that support it. So it's fine by me ;)

Leave a comment