【问题标题】:Virtual friend functions and template explicit specializations虚拟朋友函数和模板显式特化
【发布时间】:2011-02-22 14:27:22
【问题描述】:

我正在尝试通过使用受保护的虚拟成员函数 (http://www.parashift.com/c++-faq-lite/friends.html#faq-14.3) 来模拟虚拟朋友函数的效果。 此外,我为我的模板类使用了显式的特化。我想知道是否有人可以阐明我做错了什么。 下面,我发布了带有编译器错误的代码。欢迎任何建议!

foo.h

template < class T, class U >
class CFoo;

template < class T, class U >
void applyOnBar( const CFoo<T, U> &, const CBar<U> );

template < class T, class U >
class CFoo{

    public:
        .....
        friend void applyOnBar< >( const CFoo<T, U> &, const CBar<U> & ); // error, line 28: function does not match any template declaration.
        .....
        virtual ~CFoo();

    protected:

        virtual void do_applyOnBar(const CBar<U> &);
        .....
};

foo.cpp

template < class T, class U >
void applyOnBar( const CFoo<T, U> & refFoo, const CBar<U> & refBar ){
    refFoo.do_applyOnBar(refBar); // error, line 36: passing argument first argument discards qualifiers.
}

template < class T, class U >
void CFoo<T, U>::do_applyOnBar( const CBar<U> & refBar ){  // error, line 40: member function is protected.
    ......
}
#include "../impl/foo-impl.inc"

foo-impl.inc

template class CFoo<float, float>; // line #1
.....
template void applyOnBar(const CFoo<float, float> &, const CBar<float> &); // line #6

./inc/foo.h: In instantiation of ‘CFoo<float, float>’:  
./src/../impl/foo-impl.inc:1:   instantiated from here  
./inc/foo.h:28: error: template-id ‘applyOnBar<>’ for ‘void applyOnBar(const CFoo<float, float>&, const CBar<float>&)’ does not match any template declaration  

./src/foo.cpp: In function ‘void applyOnBar(const CFoo<T, U>&, const CBar<U>&) [with T = float, U = float]’:  
./src/../impl/foo-impl.inc:6:   instantiated from here  
./src/foo.cpp:40: error: ‘void CFoo<T, U>::do_applyOnBar(const CBar<U>&) [with T = float, U = float]’ is protected  
./src/foo.cpp:36: error: within this context  
./src/foo.cpp:36: error: passing ‘const CFoo<float, float>’ as ‘this’ argument of ‘void CFoo<T, U>::do_applyOnBar(const CBar<U>&) [with T = float, U = float]’ discards qualifiers

【问题讨论】:

    标签: c++ templates inheritance virtual


    【解决方案1】:
    refFoo.do_applyOnBar(refBar); // error
    

    由于refFoo是对const CFoo的引用,所以不能用于调用非常量函数。 do_applyOnBar 是一个非常量成员函数。

    因此,要么通过在函数签名右侧写关键字 const 使 d_applyOnBar 成为 const 函数,要么通过删除参数声明中的 const 限定符使 refFoo 成为非 const !

    Erik 指出了另一个问题。看看他的回答!

    【讨论】:

    • @Javier:它解决了你的问题吗?您需要更多帮助和说明?
    • @Nawaz,我不需要更改模板的类型参数。您认为为什么需要这样做?
    • @Javier:您是在谈论类型参数的 X 和 Y 名称吗?还是 CFoo 的 const-ness?
    • 我不认为你这样做,这将使所有实例化成为朋友。你只需要为你自己的类型加好友。
    • @Nawaz,我指的是XY 类型名称。
    【解决方案2】:
    template < class T, class U > void applyOnBar( const CFoo<T, U> &, const CBar<U> );
    

    第二个参数缺少 &,应该是:

    template < class T, class U > void applyOnBar( const CFoo<T, U> &, const CBar<U> & );
    

    接下来,

    virtual void do_applyOnBar(const CBar<U> &);
    

    这需要一个 const,因为您使用 refFoo.do_applyOnBar(refBar); 而 refFoo 是 const。

    virtual void do_applyOnBar(const CBar<U> &) const;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      相关资源
      最近更新 更多