rewrite to c++