Calling instance methods (again)


Just when I thought I had this sorted! Using the example we discussed a month or so ago the only difference being that I am allocating an instance of foo dynamically so

// Explicit instances
// c++
class foo
    int myMethod(int i, int j);

int main()
  // we have a chaiscript and we have a Foo, so let's use a Lambda and pass that to chaiscript
  foo* f = new foo;
  chai.add(chaiscript::fun(static_cast<void (foo::*)(int, int)>(&foo::myMethod)), "myMethod");  // add method
  chai.add(chaiscript::var(f), "f"); // add object

When I invoke


I simply get the error

"Script error in script: Error: “Can not find object: f” during evaluation at (EVAL 63, 9)

The only thing I did (slightly) differently is that I didn’t take the address since I was already passing an object pointer.

By the way, I do not get any errors adding the “f” in the first place.

What am I missing?



I’m guessing your “f.myMethod(1,2)” is actually happening inside of a function? In which case f is not in scope? if you want f to be globally accessible you’d want to do:

chai.add_global(chaiscript::var(f), "f");




Now we are rolling! Thanks so much.

Curious though — what’s the point of just ‘add’ if you can’t access what you’ve added from your functions?


The point is the repl case:

chai> var i = 2
chai> i + 3

That i is valid and available in that local top level scope.