简体   繁体   English

模板中的类的构造方法

[英]Constructor of class in template

I have a object cache class like this: 我有一个像这样的对象缓存类:

#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;
}

I known that "typename type2" was unnecessary but I need a way to pass a class object that have a constructor with parameter like class A to the template. 我知道“ typename type2”是不必要的,但我需要一种将具有构造函数(如类A)的类对象传递给模板的方法。 or their was another way to do this ? 还是他们这样做的另一种方式? please help. 请帮忙。

You can have the template parameter on the method: 您可以在方法上使用template参数:

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

Do you want something like this? 你想要这样的东西吗?

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;
}

Rather than passing an explicit value, pass in an object that creates your instance for you: 传递一个为您创建实例的对象,而不是传递一个显式值:

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_;
};

Then your object can have it's own specific creator: 然后,您的对象可以具有自己的特定创建者:

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;
}

The advantage of this approach is that you can use your cache with objects that have different numbers of arguments to their constructors. 这种方法的优点是,可以将缓存与具有不同数量构造函数的对象一起使用。

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

is wrong. 是错的。 You are supposed to indicate the types used by the objectCache class. 您应该指出objectCache类使用的类型。 It should be something like 应该是这样的

objectCache<A, A> intcache;

In the objectCache class when you instantiate objects of A, you will have to pass a parameter to its constructor. 在实例化A的对象时,在objectCache类中,必须将参数传递给其构造函数。

For example: 例如:

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

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

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