如何在Quick-Cocos2d-X中使用模板类

阅读更多
template class MyContainer : public CCObject {
public:
    MyContainer(int size) {
        T* data = new T[size];
        _data = data;
    }
    
    ~MyContainer() {
        delete _data;
    }
    const T& operator[](int index) const {
        return _data[index];
    }
    
	T& operator[](int index) {
        return _data[index];
    }
    
private:
    T* _data;
};

 

tolua 描述

class MyContainer : public CCObject {
TOLUA_TEMPLATE_BIND(T, int)

public:
		
		
    MyContainer(int size);
    
    ~MyContainer();
    
    const T& operator[](int index) const;
    
		T& operator[](int index);
};

 

在Lua中调用相应的方法:

    local cc = MyContainer_int_:new_local(4)
    cc[0] = 100
    cc[1] = 200
    log('cc value: %d, %d', cc[0], cc[1])

 

你可能感兴趣的:(lua)