首页 > 解决方案 > 与 C++ 中的 Lua 表操作最接近的是什么?

问题描述

最近得到了 C++ 和 SDL2 来编译,并开始搞乱。我已经很好地学习了 Lua 和 JavaScript,并且可以毫无问题地完成大部分我需要完成的事情。

我希望创建一个可以实现类似 Lua 示例的对象,但使用 C++。我无法在网上找到任何东西,但我可能没有使用正确的搜索词,因为我是 C++ 新手(尽管我有一些 C 经验)。

foo = {
    a = 0,
    b = "hello"
    c = "world"
}

function foo:bar(baz)
    self.a = self.a + 1
    print(self.b.." "..self.c)
    table.insert(self, baz)
end

任何帮助是极大的赞赏!

标签: c++ooplua

解决方案


一本好书开始学习 C++ 会更容易,而不是立即尝试自己玩。C++ 不像 Lua 那样容忍错误。

不过,这里有一些类似于 Lua 示例的 C++ 代码:

// Makes the standard type std::string available:
#include <string>

// Every variable needs a type.
// "class" creates a new type with given members, so here's how we'll define
// a type for variable "foo".
// A class definition like this usually goes in a header file.
class FooType
{
  public:
    // These members have default initializers, which will be used when
    // a FooType object is created without its own initializer.
    int a = 0;
    std::string b = "hello";
    std::string c = "world";

    // This declares a class member function named bar, returning nothing:
    void bar();
};

// The actual variable foo:
FooType foo;

// Defining the member function called FooType::bar :
// This might go in a source file which begins with an #include of
// the corresponding header file.
#include <iostream> // to get std::cout and std::endl
void FooType::bar()
{
    // self.a = self.a + 1 :
    // The C++ keyword "this" is similar to Lua's "self", but is a pointer.
    // All of these are equivalent:
    // (*this).a = (*this).a + 1;
    // this->a = this->a + 1;
    // a = a + 1;  // C++ automatically adds an implicit "this->" when you
                   // just name a member.
    // ++a; // A shortcut for adding one to a number.
    ++a;

    // print(self.b.." "..self.c) :
    std::cout << b << " " << c << std::endl;
    // Lua's print automatically adds a newline after its data, but
    // C++ std::cout does not. The std::endl adds a newline and then
    // "flushes" the output, making sure it gets sent out more or less
    // immediately instead of waiting for more data as an optimization.

    // table.insert(self, baz) :
    // Not possible - C++ does not allow adding members to a class which
    // aren't declared in the class definition. If you really need something
    // similar, you could use a map with name as the key, but then you would
    // need to access all those "members" via that map, not with a plain
    // obj.name expression.
}

需要注意的一些差异:

在 Lua 中,冒号方法语法只是一个捷径。也就是说,foo:bar(t)与 完全相同foo.bar(foo, t),但 Lua 会让你做一些奇怪的事情,比如foo.bar(0, t)self变量变为0。(这当然可能违反bar方法的隐含契约!)在 C++ 中,编译器通常会实现成员函数,就好像它们是带有额外参数的普通函数this一样,但就语言而言,成员函数完全是与非成员函数不同,调用它的唯一方法都涉及成为正确类型的对象*this

相关,Lua 允许您重新分配self,但 C++ 不允许您更改指针this

Lua 使用“引用语义”处理表,但 C++ 中的默认设置是所有变量都使用“值语义”。

-- Lua:
foo2 = foo     -- Another name for the same table.
foo2.a = 3     -- foo.a is now 3.
// C++ (within a function):
FooType foo2 = foo;  // A different independent object.
foo2.a = 3;          // foo2.a is 3, but foo.a is unchanged

FooType& foo3 = foo; // Another name for object foo.
foo3.a = 4;          // foo.a is now 4.

推荐阅读