Sometimes the way two languages implement a feature can significantly differ in surprising ways, and even the keywords can be misleading. This is the case of instantiation of C++classes and C#structs a.k.a. .NET value types.
In this article, we will see the differences between the two languages in the way they handle the creation of objects: their memory allocation and their initialization.
It seemed technically feasible because Python has a remarkable tool to interact with native code: the ctypes module.
The only issue is that ctypes only supports C interfaces not C++ classes so in this case it can’t directly use the YahooAPIWrapper class.
In fact it’s a minor issue because this kind of situation is well known and a well documented pattern exists to circumvent it: building a C wrapper around the C++ API.
This looks a little crazy because you now have 2 layers between the Python client code and the C# Yahoo API:
Python -> C wrapper -> C++/CLI wrapper -> C# API
So, while I don’t think this layering could have any usefulness in real-life, this was a challenging and interesting question.
Looks simple no? Well, as you know when you start to pile up heterogeneous layersunexpected issues can appear and this is exactly what happened there, and it has revealed one that is worth talking about.
So keep reading!