初学者和C++模板:是否有可能使用C++模板使一个类面向使用字符与服装结构一起工作?

Rel*_*lla 0 c++ oop templates boost class

所以我对C++很陌生,我真的不懂模板,如何使用tham thow我rad维基百科并开始阅读2000页长篇C++文章...所以我可能有点不耐烦但我不知道如果使用C++我们可以制作模板,例如这种简单的类对使用服装结构而不是字符.

#include <iostream>
#include <vector>

// Boost
#include <boost/thread.hpp>

#ifndef _IGraphElementBase_h_
#define _IGraphElementBase_h_
#pragma once

using namespace std ;

class IGraphElementBase {

public:
    boost::thread GraphWorker;
    mutable boost::mutex GraphItemMutex;
    boost::condition_variable GraphItemMutexConditionVariable;
    int SleepTime;

    // Function for preparing class to work 
    virtual void Init(){ SetSleepTime(1);}

    void SetSleepTime(int timeMS)
    {
        SleepTime = timeMS;
    }

    // Function for data update // word virtual makes it possible to overwrite it
    virtual void updateData(){}


    void StartThread()
    {
        GraphWorker = boost::thread(&IGraphElementBase::Call, this);
    }

    virtual void CleanAPI(){}

    virtual void Clean()
    {
        GraphWorker.interrupt();
        GraphWorker.join();
        CleanAPI();

    }
    virtual void CastData(){}

    //Here is a main class thread function in infinite loop it calls for updateData function
    void Call()
    {
        try
        {
            for(;;){
                boost::this_thread::sleep(boost::posix_time::milliseconds(SleepTime));
                boost::mutex::scoped_lock lock(GraphItemMutex);
                boost::this_thread::interruption_point() ;

                updateData();

                lock.unlock();
                CastData();
                GraphItemMutexConditionVariable.notify_one();
            }
        }
        catch (boost::thread_interrupted)
        {
            // Thread end
        }
    }  
};

#endif // _IGraphElementBase_h_

#include "IGraphElementBase.h"

#ifndef _IGraphElement_h_
#define _IGraphElement_h_

using namespace std ;

class IGraphElement : public IGraphElementBase{

    // We should define prototype of functions that will be subscribers to our data
    typedef void FuncCharPtr(char*, int) ;

public:
    struct GetResultStructure
    {
        int length;
        char* ptr;
    };

    // initGet sets up a pointer holding a copy of pointer of data we want to return on Get() call
    void InitGet(char * pointerToUseInGetOperations, int pointerToUseInGetOperationsSize)
    {
        pointerToGet = pointerToUseInGetOperations;
        pointerToGetSize = pointerToUseInGetOperationsSize;
    }

    // Function for adding subscribers functions
    void Add(FuncCharPtr* f)
    {
        FuncVec.push_back(f);
    }

    // Returns pointer to copy of current graphItem processed data
    GetResultStructure Get()
    {
        boost::mutex::scoped_lock lock(GraphItemMutex);
        char * dataCopy = new char[pointerToGetSize];
        memcpy (dataCopy,pointerToGet,pointerToGetSize);
        lock.unlock();
        GraphItemMutexConditionVariable.notify_one();
        GetResultStructure result;
        result.ptr = dataCopy;
        result.length = pointerToGetSize;
        return result;
    }

    void Clean()
    {
        GraphWorker.interrupt();
        GraphWorker.join();
        CleanAPI();
        //delete[] pointerToGet;
        //pointerToGet = 0;

    }

    // Cast data to subscribers and clean up given pointer
    void CastData(){
        for (size_t i = 0 ; i < FuncVec.size() ; i++){
            char * dataCopy = new char[pointerToGetSize];
            memcpy (dataCopy,pointerToGet,pointerToGetSize);
            FuncVec[i] (dataCopy, pointerToGetSize) ;}
    }

    // Cast given data to subscribers and clean up given pointer
    void CastData(char * data, int length){
        for(size_t i = 0 ; i < FuncVec.size(); i++){
            char* dataCopy = new char[length];
            memcpy(dataCopy, data, length);
            FuncVec[i](dataCopy, length);
        }
    }

private:
    // Char pointer to hold a copy of pointer of data we want to return on Get() call
    char* pointerToGet;
    int pointerToGetSize;

    // Vector to hold subscribed functions
    vector<FuncCharPtr*> FuncVec ;

};
#endif // _IGraphElement_h_
Run Code Online (Sandbox Code Playgroud)

那么简而言之,对我来说最有趣的是:

-   typedef void FuncCharPtr(char*, int) ;
-   vector<FuncCharPtr*> FuncVec ;
-   functions like void CastData(char * data, int length)
Run Code Online (Sandbox Code Playgroud)

如果有可能以某种方式使用模板使我的类与服装结构一起工作,那对我来说真的很谨慎.那么它是否可能以及如何做到这一点?

小智 5

模板是类的参数化.也就是说,而不是像一堆不同的类

class myclass_int
{
   int x;
}

class myclass_double
{
   double x;
}
Run Code Online (Sandbox Code Playgroud)

等等...

如果你能看到模式,唯一不同的是使用的类型,SO,我们将使用一种称为模板的抽象类型作为一种占位符,

class myclass_T
{
    T x;
}
Run Code Online (Sandbox Code Playgroud)

这个类不是单一类,但是整个系列.如果我们用int替换T,我们得到第一个类而T用double替换,我们得到第二个.

但是当我们实例化myclass_T时,我们必须指定实际上是什么(是int,double等等)?

所以我们将这个参数化的类定义为

template <typename T>
class myclass
{
   T x;
}
Run Code Online (Sandbox Code Playgroud)

并使用T,因为我们已经新了它真正的东西.

那个类代表了你可以组成的所有可能的类,它们使用了特定类型(我在开始时给了2个实例).

模板只是简单地定义这些类.它还有很多,但它是它们有用的基础.思考模板类的方法不是作为一个类而是作为"超级类".也就是说,一个能够采用不同表示的类.

这不是一个困难的概念但是如果你没有很多oop的经验,你可能不会真正理解为什么它们有用并且认为它们使事情变得更复杂.但是一旦你最终必须写出很多类似的类,只是所用的类型不同,那么你就会明白为什么它们如此有用(它们实际上非常强大,因为它们最终能够做得更多).