作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
有以下代码:
struct Base
{
virtual void print(int x) = 0;
virtual void print(float x) = 0;
};
struct Derived : public Base
{
template<typename T>
void print(T x)
{
std::cout<<x<<std::endl;
}
};
是否有可能做一些 C++ 黑魔法(对这些类型进行显式实例化,一些智能 using
等)来识别以下实现:
virtual void print(int x) = 0;
virtual void print(float x) = 0;
在Derived
类中的形式为:
template<typename T>
void print(T x)
最佳答案
不,没有。
您可以做的是转发到本地模板实现:
struct Derived : public Base
{
void print(int x) override { printTempl(x); }
void print(float x) override { printTempl(x); }
template <typename T>
void printTempl(T x)
{
std::cout << x << std::endl;
}
};
如果你觉得太冗长,而且你有很多这样的 print
,你也可以宏它:
#define PRINT_FWD(typ) void print(typ x) override { printTempl(x); }
关于c++ - 在派生类中作为模板实现的虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30923902/
我有一个特别的问题想要解决,我不确定是否可行,因为我找不到任何信息或正在完成的示例。基本上,我有: class ParentObject {}; class DerivedObject : publi
在我们的项目中,我们配置了虚 URL,以便用户可以在地址栏中输入虚 URL,这会将他们重定向到原始 URL。 例如: 如果用户输入'http://www.abc.com/partner ',它会将它们
我是一名优秀的程序员,十分优秀!