use C++ to implement this