最佳答案
首先,C++ 风格的接口(interface)现在在像 Lua 这样的语言中确实很有意义。对于符合接口(interface)的 Lua 对象,它只需要包含该接口(interface)中所有函数的定义。不需要任何特定的继承。例如,如果您有一个像这样的 C++ 接口(interface):
// Represents a generic bank account
class Account {
virtual void deposit(double amount) = 0;
};
您可以在 Lua 中实现它而无需任何特定的继承规范:
SavingsAccount = { balance = 0 }
SavingsAccount.deposit = function(amount)
SavingsAccount.balance = SavingsAccount.balance + amount
end
-- Usage
a = SavingsAccount
a.balance = 100
a.deposit(1000)
简而言之,您不需要 C++ 接口(interface)。如果您需要从 Lua 扩展 C++ 类的功能,您应该将其包装到 Lua 对象中,如所述here并按照说明进行“元表”继承 here .另请阅读 section on Object Oriented Programming在 Lua 手册中。
关于c++ - 使用 SWIG 在 Lua 中实现和继承 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1782337/