简体   繁体   中英

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, RtsInt16_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)
{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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