Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++11 是否可以使用派生类中的模板方法重写虚拟方法?_C++11 - Fatal编程技术网

C++11 是否可以使用派生类中的模板方法重写虚拟方法?

C++11 是否可以使用派生类中的模板方法重写虚拟方法?,c++11,C++11,我想用派生类中的模板方法覆盖基类中的虚拟方法;我只是想知道是否有任何聪明的方法或解决办法使这成为可能 #include <iostream> using namespace std; struct A { virtual void AF(int i) { std::cout << "Function in A" << std::endl; } virtual void af(int i) {

我想用派生类中的模板方法覆盖基类中的虚拟方法;我只是想知道是否有任何聪明的方法或解决办法使这成为可能

#include <iostream>
using namespace std;

struct A
{
    virtual void AF(int i)
    {
        std::cout << "Function in A" << std::endl;
    }

    virtual void af(int i)
    {
        std::cout << "Function in A" << std::endl;
    }
};

struct B
{
    virtual void BF(int i)
    {
        std::cout << "Function in B" << std::endl;
    }

    virtual void bf(int i)
    {
        std::cout << "Function in B" << std::endl;
    }
};

template<bool IS_A>
struct C : public A, public B
{
    template<class I>
    typename std::enable_if<std::is_same<int, I>::value && IS_A,void>::type AF(I i)
    {
        std::cout << "Function override from A in C" << std::endl;
    }
    template<class I>
    typename std::enable_if<std::is_same<int, I>::value && !IS_A,void>::type BF(I i)
    {
        std::cout << "Function override from B in C" << std::endl;
    }
    template<class I>
    void af(I i)
    {
        std::cout << "Function override from A in C" << std::endl;
    }
    template<class I>
    void bf(I i)
    {
        std::cout << "Function override from B in C" << std::endl;
    }
};


int main() {
    int i(0);
    {
        A * a = new C<true>();
        a->AF(i);
        a->af(i);
    }
    {
        B * b = new C<false>();
        b->BF(i);
        b->bf(i);
    }
    return 0;
}
#包括
使用名称空间std;
结构A
{
虚空AF(int i)
{

std::cout您可以使用CRTP习惯用法来实现这一点:

#include <iostream>
#include<type_traits>

struct A
{
    virtual void AF(int)
    {
        std::cout << "Function in A" << std::endl;
    }

    virtual void af(int)
    {
        std::cout << "Function in A" << std::endl;
    }
};

struct B
{
    virtual void BF(int)
    {
        std::cout << "Function in B" << std::endl;
    }

    virtual void bf(int)
    {
        std::cout << "Function in B" << std::endl;
    }
};

template<typename D>
struct CRTP: public A, public B
{
    template<typename I> void CAF(I i) { A::AF(i); }
    template<typename I> void af(I i) { A::af(i); }
    template<typename I> void CBF(I i) { B::BF(i); }
    template<typename I> void cbf(I i) { B::bf(i); }

    void AF(int i) override { static_cast<D*>(this)->CAF(i); }
    void af(int i) override { static_cast<D*>(this)->caf(i); }
    void BF(int i) override { static_cast<D*>(this)->CBF(i); }
    void bf(int i) override { static_cast<D*>(this)->cbf(i); }
};

template<bool IS_A>
struct C : CRTP<C<IS_A>>
{
    template<class I>
    void CAF(I i)
    {
        std::cout << "Function override from A in C" << std::endl;
    }
    template<class I>
    void CBF(I i)
    {
        std::cout << "Function override from B in C" << std::endl;
    }
    template<class I>
    void caf(I i)
    {
        std::cout << "Function override from A in C" << std::endl;
    }
    template<class I>
    void cbf(I i)
    {
        std::cout << "Function override from B in C" << std::endl;
    }
};

int main()
{
    int i(0);
    {
        A * a = new C<true>();
        a->AF(i);
        a->af(i);
    }
    {
        B * b = new C<false>();
        b->BF(i);
        b->bf(i);
    }
    return 0;
}
#包括
#包括
结构A
{
虚空AF(int)
{
std::cout首先,要根据
IS_A
来“启用/禁用”成员函数,您可以定义C的
true
false
专门化:

template <bool IS_A>
struct C;

template <>
struct C<true> : A, B
{
    // functions only defined for IS_A == true
};

template <>
struct C<false> : A, B
{
    // functions only defined for IS_A == false
}
模板
结构C;
模板
结构C:A,B
{
//仅为IS_A==true定义的函数
};
模板
结构C:A,B
{
//仅为IS_A==false定义的函数
}
要引入模板的覆盖,可以覆盖A和B中的模板,其中称为模板:

void af(int i) override { af<int>(i); }
void bf(int i) override { bf<int>(i); }
void af(inti)覆盖{af(i);}
void bf(int i)重写{bf(i);}

有一个区别:如果4个函数中的一个没有在C中重写,这不是在a或B中调用的函数,这是CRTP,您有一个无限递归!@O'Neil Updated。没有更多的无限递归。如果您只为a和B的相同
int
启用C::AF和C::BF的模板,那么使用它们的目的是什么?