How do I add code automatically to a derived function in C++

Posted by Ian on Stack Overflow See other posts from Stack Overflow or by Ian
Published on 2011-01-05T01:01:01Z Indexed on 2011/01/05 2:53 UTC
Read the original article Hit count: 216

Filed under:

I have code that's meant to manage operations on both a networked client and a server, since there is significant overlap between the two. However, there are a few functions here and there that are meant to be exclusively called by the client or server, and accidentally calling a client function on the server (or vice versa) is a significant source of bugs.

To reduce these sorts of programming errors, I'm trying to tag functions so that they'll raise a ruckus if they're misused. My current solution is a simple macro at the start of each function that calls an assert if the client or server accesses members they shouldn't. However, this runs into problems when there are multiple derived instances of classes, in that I have to tag the implementation as client or server side in EVERY child class.

What I'd like to be able to do is put a tag in the virtual member's signature in the base class, so that I only have to tag it once and not run into errors by forgetting to do it repeatedly. I've considered putting a check in a base class implementation and then referring to it with something like base::functionName, but that runs into the same issue as far as needing to manually add the function call to every implementation. Ideally, I'd be able to have parent versions of the function called automatically like default constructors do.

Does anybody know how to achieve something like this in C++? Is there an alternate approach I should be considering?

Thanks!

© Stack Overflow or respective owner

Related posts about c++