luabind::对象的赋值运算符是如何重载的?
How is luabind::object's assignment operator overloaded?
我正在学习luabind,并尝试使用luabind::object从C++访问Lua中的变量。
当我给一个"对象"分配一个int时,编译失败了。
守则:
int main()
{
using namespace luabind;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
open(L);
luaL_dofile(L, "./test.lua"); // a = 5
object g = globals(L);
object num = g["a"];
num = 6; // Failed to compile
return 0;
}
错误消息:
luatets.cpp:21:6: error: no match for ‘operator=’ (operand types are ‘luabind::adl::object’ and ‘int’)
num = 6;
/usr/include/luabind/object.hpp:731:9: note: luabind::adl::object& luabind::adl::object::operator=(const luabind::adl::object&)
class object : public object_interface<object>
/usr/include/luabind/object.hpp:731:9: note: no known conversion for argument 1 from ‘int’ to ‘const luabind::adl::object&’
但在我把这两行代码组合在一起后,代码就起作用了:
g["a"] = 6;
我不知道为什么会发生这种事。在luabind文档中,据说:
当您有一个Lua对象时,可以使用赋值运算符(=)为其指定一个新值。执行此操作时,default_policy将用于将C++值转换为Lua。
在类声明中,赋值运算符被重载用于任意类型,而不仅仅是对象&:
template <class T>
object& operator=(T const&);
object& operator=(object const&);
顺便说一句,我发现我的问题和这个类似,但没有人回答
我看了一下luabind标头,但在那些可怕的代理中找不到任何线索。
有人能告诉我为什么第一个代码不正确,operator=(T const &)
是否过载吗?
非常感谢!!
Luabind 0.9.1在对象中没有这样的运算符=重载,不管(可能已经过时了?!)文档怎么说。事实上,对象根本没有运算符=重载(也没有对象接口,对象是从中派生的)。
然而,它有:
template<class T>
index_proxy<object> operator[](T const& key) const
这就是g["a"] = 6;
工作的原因,因为index_proxy具有:
template<class T>
this_type& operator=(T const& value)
可以为int.
相关文章:
- 你能重载对象变量名本身返回的内容吗
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 运算符重载 += 添加新对象
- 有没有办法将重载的类函数绑定到函数对象?
- 使用操作重载对象重新分配对象
- C++内联运算符重载中,请参阅当前对象
- C++运算符重载做双 * 对象
- 尝试重载输出运算符时,我无法遍历对象向量
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 重载加法运算符(使用函数向对象添加字符串)
- 临时对象上的运算符重载
- 如何使用运算符+重载函数正确返回对象?
- 对象内部对象的重载功能
- 重载对象成员的比较运算符
- 重载对象前面带有数字的运算符+(int)
- 变量参数列表如何在C++中处理重载对象
- 我尝试重载对象的 >> 运算符,但出现一些错误。为什么?C++
- 运算符重载-对象的类型限定符与成员函数C++不兼容