How to register C++ modules
In this section, we will export a C++ class instance to Lua. You probably have used or even implemented Lua modules before, the ones that the Lua interpreter can find and load automatically and return via Lua’s require
function. Here, the focus is integrating Lua into C++, and in such use cases, things are initiated from a C++ executor to benefit from the rest of your C++ application. So, there is a difference if you have used standalone Lua modules before.
In the previous chapter, we implemented a Lua class called Destinations
to keep track of places we want to go. Let us reimplement it in C++ so that we can export it to Lua.
Implementing a C++ class
Create two source files, Destinations.h
and Destinations.cc
. Remember to add Destinations.cc
to the Makefile
. Write the header file as follows:
#ifndef _DESTINATIONS_H #define _DESTINATIONS_H #include <map> #include <vector> #include <string> class Destinations { public...