在 C++ 中通过模板参数传递类构造函数

发布于 2024-12-25 04:39:15 字数 715 浏览 2 评论 0原文

我知道函数可以通过 template 参数传递,我可以像这样传递类构造函数吗?

更新: 我想要这样做的全部原因是我可以在内存池中选择构造函数,并且无需在我想要分配的类中更改任何代码(在本例中为class A

class A
{
public:
  A(){n=0;}
  explicit A(int i){n=i;}

private:
  int n;
};

class MemoryPool
{
public:
   void* normalMalloc(size_t size);
   template<class T,class Constructor>
   T* classMalloc();
};

template<class T,class Constructor>
T* MemoryPool::classMalloc()
{
   T* p = (T*)normalMalloc(sizeof(T));
   new (p) Constructor; // choose constructor
   return p;
}

MemoryPool pool;
pool.classMalloc<A,A()>(); //get default class
pool.classMalloc<A,A(1)>();

I know function can pass through template argument, can I pass class Constructor like this.

Update:
The whole reason that I want to do this, is I can choose constructor in memory pool and without any code changing in the class I want to alloc (in this case class A)

class A
{
public:
  A(){n=0;}
  explicit A(int i){n=i;}

private:
  int n;
};

class MemoryPool
{
public:
   void* normalMalloc(size_t size);
   template<class T,class Constructor>
   T* classMalloc();
};

template<class T,class Constructor>
T* MemoryPool::classMalloc()
{
   T* p = (T*)normalMalloc(sizeof(T));
   new (p) Constructor; // choose constructor
   return p;
}

MemoryPool pool;
pool.classMalloc<A,A()>(); //get default class
pool.classMalloc<A,A(1)>();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹过旳痕迹 2025-01-01 04:39:15

您不能传递构造函数,但可以传递工厂函子:

class A
{
    int n;

    A(int i) : n(i) {};

public:

    static A* makeA(int i)
    {
        return new A(i);
    }
};

template<typename T, typename Factory>
T* new_func(Factory factory)
{
    return factory();
}

#include <functional>

int main()
{
    new_func<A>(std::bind(&A::makeA, 0));
    new_func<A>(std::bind(&A::makeA, 1));
}

You cannot pass around constructors, but you can pass around factory functors:

class A
{
    int n;

    A(int i) : n(i) {};

public:

    static A* makeA(int i)
    {
        return new A(i);
    }
};

template<typename T, typename Factory>
T* new_func(Factory factory)
{
    return factory();
}

#include <functional>

int main()
{
    new_func<A>(std::bind(&A::makeA, 0));
    new_func<A>(std::bind(&A::makeA, 1));
}
简单气质女生网名 2025-01-01 04:39:15

你的整个假设都是错误的。你不需要那个功能。

template<class T>
T* new_func()
{
   return new T;
}

new 之后的东西是类型,而不是构造函数引用。

Your whole assumption is wrong. You don't need that feature.

template<class T>
T* new_func()
{
   return new T;
}

The thing after new is a type, not a constructor reference.

鲜血染红嫁衣 2025-01-01 04:39:15

我认为这样更好

template<class T, int n>
struct Factory
{
  static T* new_func()
  {
     return new T(n);
  }
};

template<class T>
struct Factory<T,0>
{
  static T* new_func()
  {
     return new T;
  }
};

T* t = Factory<T>::new_func(); //call default constructor
T* t2 = Factory<T,2>::new_func(); //call constructor T(2)

This way better I think

template<class T, int n>
struct Factory
{
  static T* new_func()
  {
     return new T(n);
  }
};

template<class T>
struct Factory<T,0>
{
  static T* new_func()
  {
     return new T;
  }
};

T* t = Factory<T>::new_func(); //call default constructor
T* t2 = Factory<T,2>::new_func(); //call constructor T(2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文