九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
c

An example in the tutorial section : "Defining Metamethods for a C Type" looks as follows:

local ffi = require("ffi")ffi.cdef[[typedef struct { double x, y; } point_t;]]local pointlocal mt = {  __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,  __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,  __index = {    area = function(a) return a.x*a.x + a.y*a.y end,  },}point = ffi.metatype("point_t", mt)local a = point(3, 4)

I'm a bit confused about where the "constructor" is, I assume by default point(3,4) is implicity routing 3 -> x and 5 -> y. What about when I want to hang some logic onto the constructor ? Put differently.. How do I specify a non-default constructor ?

I'm wrapping a bunch of c-libraries into object oriented lua code, and I do not care about portability to canonical lua. Specifically I need to hook in the three core functions of object oriented programming for object lifetime management, create , init, destroy. I know destroy will be the __gc method of my types' metatable. So I need to know how to do create and init, and hopefully avoid a default initialization done by luajit.

edit

ffi.new and others have a bunch of rules governing the creation of types (documented in the luajit's ffi semantics page). it's in the semantics section. I'd still like to know what the cleanest way would be to hang custom creators and initializers (that come as part of a c library) into ffi object creation.

asked Feb 21 '12 at 17:41
Hassan Syed
10.2k2583

add comment

1 Answer

up vote 3 down vote accepted

You'd need to wrap your point call to get what you desire:

local function newpoint ( vals )    -- Do stuff with vals here?    return point ( vals )endnewpoint {x=5;y=4}

OR you could consider your point function as your create function; and just have an init method...

mt.__index.init = function ( p , x , y )     p.x = x;     p.y = y;endlocal mypoint = point()mypoint:init ( 1 , 2 )

Note; all objects of the point type already have your metatable applied, so you dont need to attach methods or anything.

It does seem a bit pointless to me.... why do you want to seperate creation and initialization??

answered Feb 21 '12 at 20:48
daurnimator
1,843416


 
Hahaha yes, pointless, it kept running through my head. It's a class that creates BSON / JSON objects. There is a init method to reset the class after a document has been created. Then there is a malloc/free create destroy. being a C -> C++ -> lua programmer, I got the deer in the headlight thing :D –  Hassan Syed Feb 22 '12 at 1:36
1  
Small helpful addition: make the init method return the object it's called on, e.g: mt.__index.init = function(p,x,x)p.x,p.y=x,y return p end, so you can do this: mypoint = point():init(1, 3). Not very significant, but quite handy (chaining can make code very neat). –  Deco Feb 22 '12 at 12:13

 
Thank you deco, every idiom helps :D –  Hassan Syed Feb 22 '12 at 12:56
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Two Methods of Creating JavaScript Objects: Prototype Inheritance and the Xerox Method
javascript的一個實例
ruby系列教材(16):Singletons and Other Constructors
js
三張圖搞懂JavaScript的原型對象與原型鏈
淺析java反射機制
更多類似文章 >>
生活服務
熱點新聞
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服