开发者

how to create template class in another class with out default constructor

I have the following template class

template <typename threadFuncParamT >
class ThreadPool
{
    // number of threads to be launced initially and added to thread pool.
    ThreadPool( pThreadFunc pFunction, RtsInt16_t minThreads, RtsInt16_t maxThreads, Rts开发者_如何转开发Int16_t maxExecCount);
};

Now i want to use object of another class

struct myStruct 
    //...
};

class MyClass 
{
private:

    ThreadPool<myStruct *> pool;
};

My question is how to create ThreadPool with constructor arguments in MyClass constructor as I don't have default constructor?


Assuming you mean "as I don't have default constructor in ThreadPool:

MyClass::MyClass()
 : pool( &thrdFunc, 7, 42, 12 )
{
}

Assuming you mean "as I don't have default constructor in MyClass:

MyClass has a default constructor as long as you either provide your own or you do not provide any other constructor. As it is in your question, it has a compiler-generated default constructor. Of course, that wouldn't compile, because the compiler-generated default constructor would simply call the default constructors of all (base classes and) data members, and the only data member pool is of a class that doesn't provide a default constructor.
So you need to provide your own default constructor, as shown in the example above.


Like any other member -- this is not specific to template instantiation types -- in the initializer list for the constructors of MyClass. For instance for the default constructor:.

MyClass::MyClass()
   : pool(myFunction, myMinThread, myMaxThread, myExeCount)
{}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜