简体   繁体   English

如何使用模板函数在类中初始化静态成员变量

[英]How to initialize a static member variable in class with template functions

As far as I found, there were articles about initializing a static variables in class templates. 据我发现,有一些关于在类模板中初始化静态变量的文章。 However, I´m using a normal class with member function templates, so I have to ask. 但是,我在使用带有成员函数模板的普通类,所以我不得不问。

In short version(not whole class definition) I have a class that look like this: 简而言之(不是整个类的定义),我有一个看起来像这样的类:

class BatchManager
{
    private:

        static std::vector<BaseBatch_P> _BATCHES;

    public:

        template <class T>
        static void placeData(T* data){

            //Loop through the entire container
            for (auto&& b: _BATCHES)
                if (b==data){
                    dynamic_cast<Batch<T>>(b)->draw(data);
                }

            //If no bach found, create a new One
            createNewBatch(data);
        }
};

However, when I want to access the member variables inside the function, it shows: undefined reference to BatchManager::_BATCHES 但是,当我要访问函数内部的成员变量时,它显示: 未定义对BatchManager :: _ BATCHES的引用

Then I´ve tried the following: keep definition in class header : 然后,我尝试了以下操作:将定义保留在类标头中:

 //BatchManager.h

 template <typename T>
 static void placeData(T* data);

And cpp file: cpp文件:

std::map<GLuint,BaseBatch_P> BatchManager::_TEXTURE_MAP;

template <typename T>
void BatchManager::placeData(T* data){

    //Loop through the entire container
    for (auto&& b: _BATCHES)
        if (b==data){
            dynamic_cast<Batch<T>>(b)->draw(data);
        }

    //If no bach found, create a new One
    createNewBatch(data);
}

It fixes the first problem, but then another appears, and it happens when I want to call my static function from the program: 它解决了第一个问题,但随后又出现了一个问题,当我想从程序中调用静态函数时会发生这种情况:

BatchManager::render(_data);

Error message looks like this: 错误消息如下所示:

undefined reference to BatchManager::placeData<DataType>(DataType*)

How can i fix this problem? 我该如何解决这个问题? Or am I doing something wrong? 还是我做错了什么?

Something like this: 像这样:

#include <vector>
#include <typeinfo>

struct BaseBatch {

    virtual const std::type_info& get_type() const = 0;
    virtual ~BaseBatch() = default;
};

template<class T>
struct Batch : BaseBatch
{

    const std::type_info& get_type() const override
    {
        return typeid(T);
    }

    void draw(T* data) {}
};

using BaseBatch_P = BaseBatch*;

class BatchManager
{
private:

    static std::vector<BaseBatch_P>& get_batches() {
        static std::vector<BaseBatch_P> _;
        return _;
    }

public:

    template <class T>
    static void placeData(T* data){

        //Loop through the entire container
        bool found = false;
        for (auto&& b: get_batches())
            if (b->get_type() == typeid(T)) {
                dynamic_cast<Batch<T>*>(b)->draw(data);
                found = true;
            }

        //If no bach found, create a new One
        if (not found) {
            get_batches().push_back(new Batch<T>);
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM