简体   繁体   中英

swig lua C++ overload constructor string and int hex

I'm using swig to create C++ objects from Lua, in this scenario I have overloaded constructor for int and std::string. Everything works fine except when I'm trying to create the object with string that contains hex value.

works_fine = MyObj("foo") -- MyObj::MyObj(std::string v) [v = "foo"]
works_fine = MyObj(3)     -- MyObj::MyObj(int v) [v = 3]

not_working = MyObj("0xAB") -- MyObj::MyObj(int v) [v = 171]

Apparently, it converts hex string to int (which is 171 in this case). Is there any way to force the hex string to be a string?

I fixed it with a workaround:

I created a new swig obj MyString and overloaded MyObj ctor for MyString instead of std::string

works_fine = MyObj("foo") -- doesn't work
works_fine = MyObj(3)     -- MyObj::MyObj(int v) [v = 3]

not_working = MyObj(MyString("0xAB")) -- MyObj::MyObj(MyString v) [v = "0xAB"]

Doesn't look clean so if anyone has any other ideas please share

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM