[C++-sig] a question about embedding python in c++

David Toneian david.toneian at gmail.com
Tue Oct 31 19:28:50 CET 2006


hi,
i would like to embed a scripting language in my (planned) c++ apps.
i've found boost.python now, and it seems to be what i need.
nevertheless, i'm still unsure whether i can do the follwoing with
boost.python:

(note that the python-related part is just pseudo-code, of course :> )



class someNumber
{
public:
  float x;

  someNumber() : x(0) {}
};
class myClass
{
public:
  int a;
  someNumber* number;

  myClass() : a(0) {number=new someNumber();}
  ~myClass(){delete number;}

  void add(const int x) {a+=x;}
};

void printHelloPython()
{
  std::cout<<"hello python";
}

int main()
{
  myClass foobar();

  revealFunctionToPython(&printHelloPython);
  revealClassToPython("someNumber");
  revealClassToPython("myClass");
  revealInstanceToPython(&foobar);
  runThatPythonScrip();

  return foobar.a+foobar.number.x;
}



Where the python code sets a to 5 directly (something like
foobar.a=5), calls printHelloPython(), sets the someNumber instance
inside foobar to 1330 and then calls foobar.add(2).
I'd expect an output of "hello python" and the returned result from
main to be 1337.

so, can i manipulate the c++ instance from within a python script? if
i can't, is there a scripting language that i can embed in my
application that is able to do the job?

thank you,
david



More information about the Cplusplus-sig mailing list