Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;类继承,并使用;获得;方法_C++_Class_Inheritance_Static Methods_Static Members - Fatal编程技术网

C++ C++;类继承,并使用;获得;方法

C++ C++;类继承,并使用;获得;方法,c++,class,inheritance,static-methods,static-members,C++,Class,Inheritance,Static Methods,Static Members,我想创建一个基类,其中大部分包含静态成员/方法,如: 类LuaObject{ 公众: 静态常数法[]; 静态int创建(lua_State*L); 静态LuaObject*get(lua_State*L,int pos){ return*(LuaObject**)luaL_checkudata(L,pos,LuaObject::Metatable); } }; 然后针对特定类型的对象从中派生: 类脉冲:公共LUAOObject{ 公众: int n; //... }; const luaL_R

我想创建一个基类,其中大部分包含静态成员/方法,如:

类LuaObject{
公众:
静态常数法[];
静态int创建(lua_State*L);
静态LuaObject*get(lua_State*L,int pos){
return*(LuaObject**)luaL_checkudata(L,pos,LuaObject::Metatable);
}
};
然后针对特定类型的对象从中派生:

类脉冲:公共LUAOObject{
公众:
int n;
//...
};
const luaL_Reg Pulse::Methods[]={
//...
};
静态int脉冲::创建(lua_状态*L){
脉冲**ud=(脉冲**)lua_newuserdata(L,sizeof(脉冲*);
*ud=新脉冲();
//...
}
然而,我不知道如何在不进行大量复制和粘贴的情况下完成这项工作(违背了
LuaObject
的目的)。出现了两个问题:

>p>错误:ISC+C++不允许'LuaObjult::方法定义为“Pult::Fieldisty[< /Cord>]和Error:没有'int Pult::Cudio(LuauStAt*)”成员函数,在类“Pult'”< /> >中声明。i、 e.似乎
Pulse
没有从
LuaObject
继承静态成员/方法

  • get(L,n)
    方法用于返回指向类实例的指针,例如,我希望能够编写:

    Pulse *p = Pulse::get(L,1); printf("p->n = %d\n", p->n); 但即使这样也会出现同样的错误,所以我很困惑

    编辑:根据要求,一个最小的示例程序,包括编译错误:(使用
    g++test.cpp-Llua-Lpulse-o test构建)

    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    类LuaObject{
    公众:
    静态常量字符*元表;
    静态常数法[];
    静态无效初始化模块(lua_状态*L);
    静态int创建(lua_State*L);
    //从指定位置的Lua堆栈检索对象实例
    静态LuaObject*get(lua_State*L,int pos){
    return*(LuaObject**)luaL_checkudata(L,pos,LuaObject::Metatable);
    }
    };
    类脉冲:公共LuaObject{
    //在这里,我希望避免再次声明/定义元表、get()等。
    公众:
    静态int getEvent(lua_State*L);
    pa_上下文*ctx;
    };
    常量字符*脉冲::元表=“脉冲”//错误:ISC++不允许“LuaObjult:Meta”定义为“Pult::Meta”[fPixime]。
    const LualueReg脉冲::方法[]= {//Orth:ISO C++不允许'LuAbOb::MultRead定义为“Pult::方法”[fimix]
    {“getEvent”,脉冲::getEvent},
    {NULL,NULL}
    };
    //将“脉冲”模块设置为Lua状态
    静态void Pulse::InitModule(lua_State*L){//错误:在类“Pulse”中未声明“void Pulse::InitModule(lua_State*)”成员函数
    if(luaL_newmetatable(L,Pulse::Metatable)){
    luaL_setfuncs(L,Pulse::Methods,0);
    }
    lua_pop(L,1);//从lua堆栈中删除元表,保留在lua注册表中
    静态luaL_Reg lib[]={
    {“新建”,脉冲::创建},
    {NULL,NULL}
    };
    luaL_newlibtable(L,lib);
    lua_setglobal(L,“脉冲”);
    }
    //从Lua调用(作为pulse.new())以创建pulse对象
    静态int Pulse::Create(lua_State*L){//错误:在类'Pulse'中没有声明'int Pulse::Create(lua_State*)'成员函数
    脉冲**ud=(脉冲**)lua_newuserdata(L,sizeof(脉冲*);
    *ud=新脉冲();
    返回1;
    }
    //从Lua调用(作为myPulse:getEvent())来读取事件
    int Pulse::getEvent(lua_State*L){
    //我们只有L作为参数,脉冲对象在Lua堆栈上,
    //因此,我们需要使用luaL_checkudata()读取它(这是经过处理的)
    //通过get())
    Pulse*self=Pulse::get(L,1);//错误:从'LuaObject*'到'Pulse*'的转换无效[-fpermissive]
    //假装get_pulse_event()是在pulseaudio.h(实际机制)中定义的
    //要复杂得多,对示例没有帮助)
    int事件=获取脉冲事件(自->ctx);
    lua_pushinteger(L,事件);
    返回1;
    }
    int main(int argc,字符**argv){
    lua_State*L=luaL_newstate();
    luaL_openlibs(L);
    脉冲:初始模块(L);
    返回luaL_dofile(L,“main.lua”);
    }
    
    必须为base定义base中的静态成员。如果要为派生类定义它们,则必须再次声明它们。这并不意味着您不能访问派生的静态成员,尽管您必须知道静态成员不能声明为
    virtual


    顺便说一句:
    static
    只能在类定义中指定

    你想做的事不会按你的想法去做

    class ClassWithoutStaticMembers {
    private:
        int myInt; 
    public:
       void someFunc();
    };
    
    void ClassWithoutStaticMembers::someFunc() {
        // Here we can access member variable myInt in two ways:
        myInt = 10; // directly
    
        this->myInt = 12; // Using the pointer (this) that belongs to the class.
    }
    
    // When you declare functions that have static storage space
    // They do not have the class's (this) pointer associated with them
    
    class ClassWithStaticMembers {
    public:
        static int x;
        void someFunc();
        static void someStaticFunc();
    };
    
    void ClassWithStaticMembers::someFunc() {
        x = 3; // valid
        this->x = 9; // valid because x does belong to this class but is of static storage type
    }
    
    void ClassWithStaticMembers::someStaticFunc() {
        this->(anything); // not valid this pointer is not associated with this static member function.
    } 
    
    // You can still inherit from the second class, but the static member
    // function is not defined in your derived classes. However you
    // can still access that function either through an object
    // or without referencing that object.
    
    class DerivedA : public ClassWithStaticMembers {
    public:
        void anotherFunc();
    };
    
    int main() {
        DerivedA a;
        a.someStaticFunc(); // this is valid;
        return 0;
    }
    
    int main() {
        // Here we do not declare a variable of type ClassWithStaticMembers 
        // or DerivedA but we can access ::someStaticFunc() from either since 
        // it is declared public in the base class
    
        ClassWithStaticMembers::someStaticFunc(); // We invoke this function call - valid.
        DerivedA::someStaticFunc(); // We Invoke this function again because
        // DerivedA inherits publicly from ClassWithStaticMembers.
        return 0;
    }
    
    // Now if we go back to the class definition of DerivedA and look at its member function anotherFunc();
    void DerivedA::anotherFunc() {
        // Here we can use DerivedA this pointer to access the static function
        // because it is a member of this class's base type
        this->someStaticFunc(); // This is valid and will invoke the static function call
    }
    

    隐马尔可夫模型。。。为什么首先需要一个包含所有静态方法的对象?也许那些静态方法应该只是独立的方法。有点像是在标准库中使用容器和算法完成的。另外,
    get
    方法是静态的,这看起来很奇怪。这看起来更像是一个设计问题。尽管如此,这个问题,为什么你的尝试不起作用,还是很有趣的。这个想法是为了继承类,比如
    Pulse
    ,都有相同的方法,而不必重复它们。我不知道还能怎么做<需要code>get,因为Lua只给出一个
    void*
    。(我想我可以选演员,但那有点难看)不,不,不,我不是问继承的问题。我在问为什么一切都是静止的。这令人困惑。像
    get
    这样的东西应该绑定到一个实例,至少这是我的直觉。您还应该发布一个最小的冒犯性示例。似乎您在派生类中重新声明了一个静态成员,并弄糟了更多的事情。
    get
    不能绑定到实例,因为我们首先是通过这种方式检索实例的。当Lua调用我们的方法时,它只传递一个
    Lua\u状态*
    ,我们必须检索实际参数(例如
    脉冲)*