About ChaiScript

ChaiScript is the first and only scripting language designed from the ground up with C++ compatibility and modern design in mind. It is an ECMAScript-inspired, embedded functional-like language.

ChaiScript is licensed under the BSD license.

Download

Due to an increased release schedule, and the nature of ChaiScript being a header-only library, binary releases will be made less often. Previous releases can be found on github.

Version: 4.2.0 Released: 12/1/2012
(Requires Boost)
Source (tar.bz2)
Source (zip)

Version: 5.2.0 Released: 12/1/2012
(Requires C++11 Compiler)
Source (tar.bz2)
Source (zip)

Returning reference and manipulate it

I have code:

#include <iostream>
#include <chaiscript/chaiscript.hpp>
 
using namespace std;
using namespace chaiscript;
 
class B
{
public:
    B() { value = 1; }
    ~B() {}
    void setValue(int val) { value = val; }
    int getValue() const { return value; }
private:
    int value;
};
 
class A
{
public:
    A() {}
    ~A() {}
 
    void setB(B* b) { b_ = b; }
    B* getB() const { return b_; }
private:
    B* b_;
};
 
void
chaiInit(ChaiScript &chai)
{
    chai.add(user_type<A>(), "A");
    chai.add(constructor<A()>(), "A");
    chai.add(constructor<A(const A&)>(), "A");
    chai.add(fun(&A::operator=), "=");
    chai.add(fun(&A::getB), "getB");
    chai.add(user_type<B>(), "B");
    chai.add(constructor<B()>(), "B");
    chai.add(constructor<B(const B&)>(), "B");
    chai.add(fun(&B::operator=), "=");
    chai.add(fun(&B::getValue), "getValue");
    chai.add(fun(&B::setValue), "setValue");
}
 
int main()
{
    ChaiScript chai;
    chaiInit(chai);
    A a;
    B b;
    a.setB(&b);
    chai.add(var(&a), "a");
    chai.add(var(&b), "b");
    cout << "Script output: " << endl;
    chai.eval_file("chai.chai");
    cout << "Other output" << endl;
    cout << "B->value: " << b.getValue() << endl;
    return 0;
}

And script:

var objB = a.getB()
b.setValue(2)
a.getB().setValue(3)
var value = a.getB().getValue()
 
print(value)
print(objB.getValue())

Output:

Script output: 
3
1
Other output
B->value: 3

Can i store in objB a reference of b? In this case it copy a "B" object. I think...
(Tested on version 4.0.0)

-----
Sorry for bad English :)

[CLOSED]

[CLOSED]
Sorry. Need to do:
var objB := a.getB()
instead of:
var objB = a.getB()

Glad you got it sorted out,

Glad you got it sorted out, let us know if you have any other questions.

Support ChaiScript by sharing it with your friends: