簡體   English   中英

模板中的類的構造方法

[英]Constructor of class in template

我有一個像這樣的對象緩存類:

#include "boost/thread/mutex.hpp"
#include "boost/unordered_map.hpp"

template <typename type1, typename type2>
class objectCache
{
public:
    objectCache()
    {
        IDCounter = 0;
    }
    ~objectCache()
    {
        for ( it=free_objects.begin() ; it != free_objects.end(); it++ )
            delete (*it).second;
        for ( it=busy_objects.begin() ; it != busy_objects.end(); it++ )
            delete (*it).second;
    }
    type1* objectCache::Get()
    {
        boost::mutex::scoped_lock(io_mutex);
        if(free_objects.size() > 0)
        {
            it = free_objects.begin();
            type1 *temp = (*it).second;
            busy_objects[(*it).first] = temp;
            free_objects.erase(free_objects.begin());
            return temp;
        }
        type1 * temp = new type2;
        ++IDCounter;
        busy_objects[IDCounter] = temp;
        return temp;
    }
    void objectCache::Pushback(type1)
    {
        boost::mutex::scoped_lock(io_mutex);
        free_objects[ID] = socket;
        it = busy_objects.find(ID);
        busy_objects.erase(it);
    }
protected:
private:
    boost::mutex io_mutex;
    long long IDCounter;
    boost::unordered_map<long long, type1*> free_objects;
    boost::unordered_map<long long, type1*> busy_objects;
    typename boost::unordered_map<long long, type1*>::iterator it;
};

class A{
public:
    A(int num){
        number = num;
    }
    int number;
};
int main(int argc, char* argv[])
{
    objectCache<a, a(1)> intcache;
    A* temp = intcache.Get();
    cout <<temp->number <<endl;
    system("pause");
    return 0;
}

我知道“ typename type2”是不必要的,但我需要一種將具有構造函數(如類A)的類對象傳遞給模板的方法。 還是他們這樣做的另一種方式? 請幫忙。

您可以在方法上使用template參數:

template <typename type2>
type1* objectCache::Get()

你想要這樣的東西嗎?

template <typename type1>
class objectCache
{

// ...

template<typename type2>
type1* Get(type2 value)
{
    boost::mutex::scoped_lock(io_mutex);
    if(free_objects.size() > 0)
    {
        it = free_objects.begin();
        type1 *temp = (*it).second;
        busy_objects[(*it).first] = temp;
        free_objects.erase(free_objects.begin());
        return temp;
    }
    type1 * temp = new type1(value);
    ++IDCounter;
    busy_objects[IDCounter] = temp;
    return temp;
}

// ...

};

int main(int argc, char* argv[])
{
    objectCache<A> intcache;
    A* temp = intcache.Get(1);
    cout << temp->number << endl;
    system("pause");
    return 0;
}

傳遞一個為您創建實例的對象,而不是傳遞一個顯式值:

template <typename type1>
struct DefaultInstanceCreator {
  type1 * operator ()() const {
    return new type1;
  }
};

template < typename type1
         , typename InstanceCreator = DefaultInstanceCreator<type1> >
class objectCache {
public:
  objectCache (InstanceCreator const & instCreator)
    : instCreator_ (instCreator)  {
  }
  type1* Get() {
    type1 * temp = instCreator_ ();
  }
private:
  InstanceCreator instCreator_;
};

然后,您的對象可以具有自己的特定創建者:

class A {
public:
    A(int num){
        number = num;
    }
    int number;

public:
  struct CreateInstance  {
    CreateInstance (int value)
      : value_ (value) {
    }
    A * operator ()() const {
      return new A(value_);
    }
    int value_;
  };
};

int main(int argc, char* argv[]) {
  objectCache<A, A::CreateInstance> intcache ( A::CreateInstance (1) );
  A* temp = intcache.Get();
  return 0;
}

這種方法的優點是,可以將緩存與具有不同數量構造函數的對象一起使用。

int main(int argc, char* argv[])
{    
    objectCache<a, a(1)> intcache;
    ...
}

是錯的。 您應該指出objectCache類使用的類型。 應該是這樣的

objectCache<A, A> intcache;

在實例化A的對象時,在objectCache類中,必須將參數傳遞給其構造函數。

例如:

...
type1 * temp = new type1(1);
...    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM