Comments (1)
Hi,
It's not able to call a non-static method without an instance in C++, I mean you can't simply do that. But we are still able to do it with some tricks. The problem is C++ needs to know which instance a method will execute with, so we can store an instance and tell C++.
With MY-BASIC, for example, assume we got a C++ class:
class AClass {
public:
void foo(void) const;
};
Now I'd define some bridge function:
int _a_new_instance(mb_interpreter_t* s, void** l) {
// Creates an instance of AClass.
mb_value_t ret;
...
mb_check(mb_attempt_open_bracket(s, l));
mb_check(mb_attempt_close_bracket(s, l));
ret.type = MB_DT_USERTYPE;
ret.value.usertype = (void*)(new AClass);
...
}
int _a_foo_bridge(mb_interpreter_t* s, void** l) {
// Calls the foo method of an AClass instance.
mb_value_t ret;
AClass* inst = NULL;
...
mb_check(mb_attempt_open_bracket(s, l));
mb_check(mb_pop_value(s, l, &a));
mb_check(mb_attempt_close_bracket(s, l));
inst = (AClass*)a.value.usertype;
inst->foo();
...
}
Don't forget to register them. Then in MY-BASIC code:
a = a_new_instance()
a_foo_bridge(a) ' Calls the foo method.
This is not the only way to call a non-static C++ method with C, it's just a simple practice. You could find a proper way by searching something like "call c++ method from c". There are also many ways to store a non-static method and its corresponding instance in a variable, you may would like to take a look at "function bind" if you were using C++11, or use the famous very lightweight Fastdelegate lib for C++.
BTW. MB_DT_USERTYPE_REF is more intelligent than MB_DT_USERTYPE, because MY-BASIC can manage the deallocation of a referenced object with Reference Counting and GC.
Best
WRX
from my_basic.
Related Issues (20)
- Add real/float symbol to distinguish between Integer
- Write data to file HOT 2
- negative numbers HOT 1
- Function Overloading HOT 2
- Select Case feature
- Vector Workflow HOT 1
- Very slow ops on Strings HOT 2
- Very slow FOR HOT 1
- Spaces on certain keywords? HOT 2
- Type results depends on quotes used HOT 2
- Skipping Iterator variable option HOT 1
- Class member variable default value issue
- Unicode Printing Issue HOT 5
- REM and ENDREM as comment blocks HOT 2
- Crash when setting larger array sizes
- Support functions with and without brackets
- mb_debug_count_stack_frames - undeclared 'l' HOT 1
- Memory leak ? HOT 3
- (Question) Preferred way to use print/input redirect with socket I/0 HOT 1
- Compile error for ESP32S3 in platformio
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from my_basic.