c++ - 是否可以将纯虚函数覆盖到模板?

标签 c++ templates

我不知道这是否可能: 可以说我有一个定义的接口(interface):

virtual void func()=0;

是否有可能在具体类中我像下面这样覆盖它:

template <std::size_t  number> 
void func(){ /* do something with number... */ }

谢谢!

最佳答案

is it possible that in the concrete class I override it like the following :

不,那行不通。

想想这个案例。

struct Base
{
   virtual void func()=0;
};

struct Derived : Base
{
   template <std::size_t  number> 
   void func(){}
};

Base* b = new Derived;
b->func(); // Which of the Derived::func() should that resolve to at run time?
           // Derived::func<0>()?
           // Derived::func<10>()?
           // ...

这在概念上根本没有意义。

关于c++ - 是否可以将纯虚函数覆盖到模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52887834/

相关文章:

c++ - 无法更正 C++ 代码中的错误

c++ - 开始 c++ : OOP Methodologies and more

c++ - qemu-系统-i386 : Error loading uncompressed kernel without PVH ELF Note

c++ - 按不同字段对结构队列进行排序

C++ 模板类的自继承

c++ - 如何避免与大多数相同成员函数的代码重复?

c++ - 如何使用 type_traits 生成依赖于类特化是否存在的代码?

c++ - 如何将函数传递给模板函数 A,该函数可能具有基于 A 的另一个参数的不同签名?

c++ - Windows 中的串行通信 - ClearCommError 在读取缓冲区中找不到数据

templates - Play 2.0 访问模板中的请求