Closed GoogleCodeExporter closed 8 years ago
here's the c++ code
class TObject {
public:
TObject() {
}
~TObject() {
}
void setInt(int pInt) { mInt = pInt; }
const int getInt() const { return mInt; }
private:
int mInt;
};
void wrapClasses()
{
SLB::Class<TObject>("TObject")
.constructor()
.set("setInt", &TObject::setInt)
.param("value for i")
.const_set("getInt", &TObject::getInt);
}
here's the lua code
t1 = TObject()
t1:setInt(5)
val = t1:getInt()
and here's the error message
----------------------------------
Lua Error:
test.lua:3: Unknown class i
Traceback:
[ 0 (C) ]
[ 1 (C) ] @ getInt(method)
[ 2 (main) ] hello.lua:3
Original comment by serge.ra...@gmail.com
on 20 Aug 2011 at 11:06
well it works if i change the return type to be const reference, as in
const std::string&
is that some limitation in Lua/C++ (Templates)?
Original comment by serge.ra...@gmail.com
on 21 Aug 2011 at 5:02
Indeed, is a problem with the template specialization, it doesn't get along
with "const type" , Can you be more detailed about why you need a const string,
and not a const string& or just string ?
As far as I know const string will make a copy of the string but the temporary
variable created to hold the string will be inmutable, but I don't see any
performance gain or advantage over a plain string.
Original comment by joseLuis...@gmail.com
on 5 Sep 2011 at 9:04
not a big deal for me so far, however, being forced to return a reference would
mean to keep a valid instance of that particular object around, for the
reference to stay valid.
Original comment by serge.ra...@gmail.com
on 6 Sep 2011 at 6:32
You can also not return a reference but a copy leaving the const:
int mymethod(...) instead of const int mymethod(...) or
string mymethod(...) instead of const string mymethod(...)
I don't see any advantage in returning a const COPY, but if you really need
this I can try to figure out if it's possible to do it.
Original comment by joseLuis...@gmail.com
on 6 Sep 2011 at 7:11
yeah, true.
no, as i said, no big of a deal :) ... i just wanted to know why it wouldn't
work. thanks for the answers.
Original comment by serge.ra...@gmail.com
on 6 Sep 2011 at 7:15
It's a problem with the template matching mechanism SLB relies on, type "int"
it's almost indistinguishable from "const int" at that level. :)
Original comment by joseLuis...@gmail.com
on 6 Sep 2011 at 7:20
Original issue reported on code.google.com by
serge.ra...@gmail.com
on 20 Aug 2011 at 12:00