How to implement platform specific code

I’m trying to implement some platform specific code (ContactsAndroid.h/cpp and ContactsIOS.h/mm), wrapped by a platform agnostic class (Contacts.h/cpp). I’ve had a look CCFileUtils.h/cpp and the reimplementation in android/CCFileUtilsAndroid.h/cpp and ios/CCFileUtilsIOS.h/mm, and would like to proceed similarly to this. I’m using Cocos2d-x v. 2.1.4.)

What I don’t quite get is how the compiler knows how to pick up the platform specific reimplementation. I’ve had a look at CPlatformConfig.h, and it seems significant - yet it doesn’t really seem to be the last piece of the puzzle.

Is it so simple that only Android specific source is referenced in the Android.mk file, while iOS specific source is referenced in the proj.ios/cocos2dx.xcodeproj?

So I realized that the specific CCFileUtilsAndroid.h includes the agnostic CCFileUtils.h, and, by subclassing CCFileUtils, the specific code is overriding the agnostic code in CCFileUtils. I’d like to take this opportunity to rephrase my question: how is the CCFileUtilsAndroid class used by the compiler, when only CCFileUtils.h is included from the place where it is used (say MyGame.h/cpp)?

Digging a little further, I see that CCFileUtilsAndroid.cpp implements CCFileUtils::sharedFileUtils(), which is the Android specific version. So I guess this could be a viable path in other cases too - implementing a specific factory method in the platform specific source file, while making sure that the (platform specific) project file only includes the platform specific source.