Extend the G4GeneralParticleSourceData via inheritance


I am using the G4GeneralParticleSourceData for my application, but I need some specific functionality that it is not implemented (generating primaries from the surface of a physical volume).

My approach to solve this is to construct a new ExtendedGPS class that inherits from the GPS, along with a new messenger class that inherits from the GPS messenger.

However I don’t think it is straightforward to do this (perhaps my knowledge of C++ is not enough).

In the constructor of the extended GPS, I would need to associate the new messenger, via setting the private variable theMessenger, however, since this variable is private, I cannot modify it (it would need to be set to protected).

As a less elegant solution, I could also add the new messenger to this class (along with the default one) and process my specific commands, however this would trigger the “erroneous command” logic of the default GPS from G4GeneralParticleSourceMessenger::SetNewValue, which in this case only outputs some error message, so it would work, but it also doesn’t look right.

What would be the best solution for my problem?


You can write your own Messenger class to go along with your ExtendedGPS class. In your Messenger, you would only define your own new commands, and you would pass in your subclass instances to your Messenger so it would communicate properly. The other GPS commands would still exist, and be tied to the GPS base instance.