【问题标题】:I have a template function defined for a certain class in a cpp - not hpp. Can it be used by a child class?我在 cpp 中为某个类定义了一个模板函数 - 而不是 hpp。小孩子可以用吗?
【发布时间】:2012-12-02 16:52:32
【问题描述】:

我使用以下内容将我的模板写入 cpp 而不是头文件,因为我更喜欢保持代码干净。

Storing C++ template function definitions in a .CPP file

在 .hpp
template <class obj_class> void plotLaser(int zoom, void * obj);

在.cpp
class visualizer {...};
class annotator : public visualizer {...};
template void plotLaser<visualizer>(int zoom, void * obj) {...};

我可以使用模板调用plotLaser<annotator>(...);还是必须重写它?

【问题讨论】:

  • 不,老实说,我现在正在分班,我不能再把事情搞砸了。不过我稍后会尝试,可能会使用虚拟类。

标签: c++ templates inheritance


【解决方案1】:

我认为你需要重写它。 因为你的代码

    template void plotLaser<visualizer>(int zoom, void * obj) {...};

是一个专业。

如果你需要一个注释器,你需要一个专门化的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2012-01-30
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    相关资源
    最近更新 更多