当前位置:首页 > articles > 正文内容

C++:泛型&gt>如何为一个通用函数定义多个模板解题,该函数充当静态实例创建者

Luz6个月前 (04-10)articles540

C++:Generics >> How to define several template deklarations for a generic function, which serves as an static instance creator

  • CAP 问题:
    • 你好,
    • 我正在做一些关于实例的工作,想知道是否有可能为类/结构及其派生创建一个通用的实例创建函数。我有两个问题和一个可能性问题:
      to make it work, that the generic function can at least deliever an instance of the class/struct itself, the funtion is living/existing in. --> got undefined reference errorto make it work , that the generic function can deliever several types of instances --> got undefined reference type erroris it possible to make it work, that the generic function deliever types of future derivate types, by impplementing further template declarations in these derivates. So the base class with the generic function dont need to know about them( and by my understanding of the situation, never can , because how knows what future children/derivates will follow )
  • 为了更好地理解,我准备了一个例子:
  • struct base {        base(int param1, int param2) : a(param1), b(param2){}    private:        int a,b;    public:            template <typename T> static T createInstanceGeneric(int param1, int param2){            return T(param1, param2);         }        //Thats of course working//        static base createInstance(int param1, int param2) {             return base(param1, param2);        }        void update(int param2){            _update(0, param2);        }    protected:       void _update(int param1, int param2){            a = param1;            b = param2;       }    }struct childOne : public base {    childOne(int param1, int param2) : base(param1, param2 ){}    //Thats of course working//    static base createInstanceOfBase(int param1, int param2) {             return  base::createInstance(param1, param2);        }    //Question 3//    static childOne createInstanceOfSelfViaGeneric(int param1, int param2) {             return base::createInstance<childOne>(param1, param2);        }    //Question 1 and 2//    static base createInstanceOfBaseViaGeneric(int param1, int param2) {             return  base::createInstance<base>(param1, param2);        }    void update(int param1){        base::_update(param1, 0);    }                   }void main(){    base test1 = base::createInstance(0,1); //Working    test1.update(2);    base test2 = childOne::createInstanceOfBase(0,1); //Working    test1.update(2);        base test3 = childOne::createInstanceOfBaseViaGeneric(0,1); //Question 1 and 2    test3.update(2);    childOne test4 = childOne::createInstanceOfSelfViaGeneric(0,1); //Question 3    test3.update(3);    }
  • 回答:
  • 相关文章

    使用nodejs和mongodb进行前端搜索

    Frontend search with nodejs and mongodbCaleb 问题: 我今天刚开始学习mongodb,我有一段代码可以用来查询我的数据库和搜索地址:“Park Lane 38”。这是代码,这是结果:...

    单元测试名称重要吗?

    Are unit-test names important?Xian 问题: 如果单元测试名称随着时间的推移而变得过时,如果您认为测试本身是最重要的事情,那么选择明智的测试名称是重要的吗?ie[Test]public void Sh...

    脚本1 linux中的crear usuarios和demás

    Script 1 de crear usuarios y demás en linuxMaría Isabel De la Osa Rocha 问题: Cómo se haría el script del usuario porq...

    三js如何向FBX模型添加多个没有蒙皮的Mixamo动画?

    Three.js how to add multiple Mixamo animations without skin to an FBX model?Anye 问题: I'm trying to create a game whe...

    Visual Studio GitHub链接到一个陌生人帐户

    Visual Studio GitHub is linked to a stranger accountAhmed Khaldoon 问题: 我从VisualStudio2022(我们称之为divice a)中创建了一个新的私有re...

    React、打字脚本和材料UI 4的编译问题

    Compilation issue with React, Typescript and Material-UI 4PlexQ 问题: 突然,我的整个建筑倒塌了,无法建造。几天前,我用一个新的create-react应用程序重新设置...

    发表评论

    访客

    ◎欢迎参与讨论,请在这里发表您的看法和观点。