Constructors |
![]() |
![]() |
![]() |
![]() |
Clipp can use both c++ constructors and free functions as basis to create a scripting constructor.
If we go back to our point example:
//constructor
point::point(double x,double y,double z);
//function working like a constructor
point* create_point(double x,double y,double z);
We can then expose these to the scripting in the following way:
//expose constructor
cls.constructor(arguments<double,double,double>());
//Optional syntax
cls[constructor<double,double,double>()];
//expose function working like a constructor
cls.constructor(create_point);
In javascript, a constructor can be used both in a new expression, and in a call expression
var a=new point(5,6,7); //new expression
var b=point(5,6,7); //call expression
This is the default behaviour for constructor registering today. If you want to change the constructor to be used only in new expressions, you have to add the argument construct_method to the constructor exposing function:
//Ensure that the constructor can only be used in new expressions
cls.constructor(arguments<double,double,double>(),
construct_method);
We can also expose create_point as a global function that will only be activated in call expressions:
//expose function working like a constructor
function(c,"point",create_point);
![]() |
![]() |
![]() |
Copyright © 2003-2005 Peder Holt
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.