C++ 别名没有用于更简洁调用的参数的模板函数

C++ 别名没有用于更简洁调用的参数的模板函数,c++,templates,c++14,C++,Templates,C++14,如何使用较短的语法对某个模板函数进行别名以使其被调用 来自 getSystem<SystemX>()-> //get semi-singleton instance of "SystemX" 但是,. 我觉得这不是一个使用这种黑客的地方 对不起,如果它太过期了,我对C++很陌生。 :如何? class SystemB : public SystemBase { inline auto getSystemA(void) { return getSystem<

如何使用较短的语法对某个模板函数进行别名以使其被调用

来自

getSystem<SystemX>()->    //get semi-singleton instance of "SystemX"
但是,.
我觉得这不是一个使用这种黑客的地方

对不起,如果它太过期了,我对C++很陌生。

如何?
class SystemB : public SystemBase {
    inline auto getSystemA(void) { return getSystem<SystemA>(); }
    inline auto getSystemB(void) { return getSystem<SystemB>(); }
    void fb();
};
void SystemB::fb(){
    getSystemA()->fa();
}
B类系统:公共系统库{
内联自动getSystemA(void){return getSystem();}
内联自动getSystemB(void){return getSystem();}
void fb();
};
void SystemB::fb(){
getSystemA()->fa();
}

由于您希望使用
SystemB
对象中的
SystemBase::getSystem
,因此函数位于SystemB类中

那么
getSystem()
是在基类中定义的吗?它是否返回静态对象?它有什么作用?通常,当我需要一个子对象与父对象通信时,我会使用某种引用或指针。@Dustin Goodson是的,它是在基类中定义的,如
templateT*getSystem(){…}
。它返回非静态对象,即
newsystema()
的实例。这样的实例是在游戏开始后由另一个顶级类注入的(这有点离题了,所以我就到此为止)。在
getSystem()
vs
getSystem(SystemX)
中有什么不对的呢?这对空括号伤害这么大吗?您可以使用模板参数推断,但我怀疑它会改进语法。@luk32起初,我认为不会,但在多次键入后,我相信如果有别名(仅针对此函数),我的工作效率会更高,代码的可读性也会更高。如果听起来太生疏的话,很抱歉。我认为键入getSystem()没有任何错误,除非您对几个SystemX类有100次这样的调用(这可能表明您的函数中存在一些逻辑问题)。您可以在cpp中使用别名。level保存一些按键笔划,类似于使用A=SystemA;然后你会有一些像getSystem()之类的东西。。。
#define S(param) getSystem<param>()   
S(SystemA)->fa()     
class SystemB : public SystemBase {
    inline auto getSystemA(void) { return getSystem<SystemA>(); }
    inline auto getSystemB(void) { return getSystem<SystemB>(); }
    void fb();
};
void SystemB::fb(){
    getSystemA()->fa();
}