基类中的重复出现的模板模式和静态变量 [英] Curiously Recurring Template Pattern and statics in the base class

查看:128
本文介绍了基类中的重复出现的模板模式和静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此感谢这个答案我正在考虑用CRTP实现我的问题.但是我有一个问题.在我的静态基类中,我有2组函数.一个采用std :: vectors,另一个采用标准C样式数组.因此,在基类中,我定义了一个静态函数,该函数调用non-std :: vector函数.

So thanks to this answer I'm looking at implementing my problem with CRTP. However I have a problem. In my static base class I have 2 sets of functions. One takes std::vectors and one takes a standard C-style array. So in the base class I define a static function that calls the non-std::vector function.

但是,当我从该基类派生时,我似乎不再能够访问该基类中的公共静态函数(我以为可以).

However when I derive from that base class I seem to no longer be able to access the public static function in the base class (Which I thought I could).

template< class Derived > class Base
{
public:
    static void Func( std::vector< float >& buffer )
    {
       Func( &buffer.front(), buffer.size() );
    }

    static void Func( float* pBuffer, int size )
    {
        Derived::Func( pBuffer, size );
    }
};

然后我将派生类定义如下:

I then define the derived class as follows:

class Derived : public Base< Derived >
{
public:
    static void Func( float* pBuffer, int size )
    {
        // Do stuff
    }
};

但是,当我尝试在Base类中调用静态函数时:

However when I try to call the static function in the Base class:

Derived::Func( stlVec );

从派生类中,它引发编译错误:

From the Derived class it throws a compilation error:

error C2665: 'main' : none of the 2 overloads could convert all the argument types
1>          c:\development\Base.h(706): could be 'void Func( float*, int )

我假设我将能够从Derived类中调用基类中定义的公共静态变量.但是,情况似乎并非如此……有人可以提出一种解决方法,但这并不意味着不必在我的每个派生类中实现std :: vector函数?

I assumed that I would be able to call a public static defined in the base class from the Derived class. This appears not to be the case, however ... Can anyone suggest a workaround that doesn't mean having to implement the std::vector function in every one of my Derived classes?

推荐答案

Func 隐藏所有具有 same 名称的基本成员.使用using声明式将名称从基类引入派生类.

Func in the derived class hides all base members with the same name. Use using declarative to bring the names from the base class into the derived class.

class Derived : public Base< Derived >
{
public:

    using Base<Derived>::Func;

     //rest..
};

这篇关于基类中的重复出现的模板模式和静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆