MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/overridable_function_members.cpp:6–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <iostream>
5
6int main() {
7 std::cout << "=== override-able member functions ==="
8 << std::endl;
9
10 struct thingy {
11 sol::function paint;
12
13 thingy(sol::this_state L)
14 : paint(sol::make_reference<sol::function>(
15 L.lua_state(), &thingy::default_paint)) {
16 }
17
18 void default_paint() {
19 std::cout << "p" << std::endl;
20 }
21 };
22
23 sol::state lua;
24 lua.open_libraries(sol::lib::base);
25
26 lua.new_usertype<thingy>("thingy",
27 sol::constructors<thingy(sol::this_state)>(),
28 "paint",
29 &thingy::paint);
30
31 sol::string_view code = R"(
32obj = thingy.new()
33obj:paint()
34obj.paint = function (self) print("g") end
35obj:paint()
36function obj:paint () print("s") end
37obj:paint()
38)";
39
40 lua.safe_script(code);
41
42 std::cout << std::endl;
43
44 return 0;
45}

Callers

nothing calls this directly

Calls 2

open_librariesMethod · 0.80
safe_scriptMethod · 0.80

Tested by

no test coverage detected