Inheritance problem when binding C++ native methods in Chaiscript
I use this code to validate some properties of a set of Qt objects in ChaiScript:
/// initialize the engine
boost::shared_ptr<chaiscript::ChaiScript> chai;
chai.reset(new chaiscript::ChaiScript());
std::cout << "ChaiScript engine created!!开发者_如何学JAVA!" << std::endl;
///
/// register elements
//
assert(ui->checkBox);
chai->add(chaiscript::var(ui->checkBox), "checkBox");
///
/// adapt elements
/// QCheckBox
chai->add(chaiscript::fun(&QCheckBox::isTristate), "isTristate");
chai->add(chaiscript::fun(&QCheckBox::isChecked), "isChecked");
/// validate some properties
try
{
chai->eval("print(\"Starting evaluation...\")");
int answer_i = 0;
bool answer_b = false;
//answer_b = chai->eval<bool>("checkBox.isTristate()");
answer_b = chai->eval<bool>("checkBox.isChecked()");
std::cout << "ChaiScript::: " << answer_b << " :: " << answer_i << std::endl;
}
catch(std::exception e)
{
std::cout << "ChaiScript@Exception::: " << e.what() << std::endl;
}
The problem is that the "isTristate()" call works, because this is a native method of QCheckBox. On the other hand, the "isChecked()" call fails, I think because this is an inherited method from the QAbstractButton class.
What am I doing wrong?
The latest SVN version of ChaiScript solves this problem by allowing the user to add base class relationships.
chai.add(base_class<Base, Derived>());
After which inherited function calls work as expected, without the need to clarify the base class method pointer type, as your answer shows.
The problem has been solved in the following way:
/// method binding
chai->add(chaiscript::fun<bool (QCheckBox *)>(&QCheckBox::isChecked), "isChecked");
It is necessary to specify that the bound method belongs to the QCheckBox class, in order not to bind the reference to the parent method.
Cheers!!!
精彩评论