Function Pointer Question...

Started by
3 comments, last by _Joco_ 21 years ago
Hi! I'm currently working on a scripting language for my 2d arcade game, and I am just about to finish it but I've got a little problem with function pointers. If I want to call a function from the script, wich isn't declared there, I need a pointer to the func. So I'd need a class like this : class SysFunction { char function return type list parameters function pointer } Of course the func. pointer is dependent from the first 2 variables. So my question would be how I could write a class like SysFunction wich can handle all types of functions. could someone help plz ? ...and I'm using C++... [edited by - _Joco_ on April 1, 2003 8:28:16 AM] [edited by - _Joco_ on April 1, 2003 8:28:58 AM]
Advertisement
Personally, I would create a parameter class which contains a list of all the parameters:
class Parameters{public:    const AnyType &operator [] (const int parameter_index) const    {        check parameter exists        return parameter indexed by argument;    }private:    list < AnyType >        m_parameters;};  

and then your functions called by the scripting system would have the format:
void ScriptFunction (const Parameters &params);  

and I'd defined the class AnyType to be similar to VBs Variant type (i.e. any type). If you want multiple return values (like Lua) then add a return argument list to the function parameters and have a void return.

Skizz

[edited by - Skizz on April 1, 2003 8:50:34 AM]
Actually, most dynamically-typed programming languages implemented in statically-typed languages like C and C++ use a class hierarchy to represent types, deriving all types from an abstract base class and thus allowing transparent function invocation and dispatch:
class Scr_Type{protected:  virtual Scr_Type() = 0;  virtual ~Scr_Type() = 0;  virtual std::string Type() { return typename; }  std::string typename;}; 

Or something similar. Python, for example, derives all types from PyObject. Being written in C, though, it doesn''t use OO inheritance but some properties-based mechanism (similar to how some games are data-driven).

This is similar to simply storing the typenames as strings and using object factories to invoke the appropriate constructors and translators.
Exactly. You need a function that takes a list of dynamic types and a mechanism to return zero or more values. When creating your dynamic type, add some type checking macros that can be removed in a final, bug free build by (un)defining a global preprocessor symbol. i.e. when a string is being accessed as an int, do an assert or whatever.

Skizz

P.S. Anyone else seeing that bizarre character in my earlier post. Never seen that one before. And 'Communism for Beginners' - there's something afoot here methinks.

[edited by - Skizz on April 1, 2003 9:32:49 AM]
boost.any type - generic container for a single value.
boost.tuple for returning multiple values from functions (amongst other things).

This topic is closed to new replies.

Advertisement