Reputation: 31
I have a class:
class WorkerThread
{
public:
unsigned virtual run()
{
return 0;
}
};
Defined in the header. Now in another class I create an object of this type:
WorkerThread **workerQueue;
Which is actually a pointer to pointer... OK, all good until now.
Now, how should I read this:
workerQueue = new WorkerThread*[maxThreads];
What is the meaning of the *
after the ClassName (WorkerThread
) and the array format?
Upvotes: 0
Views: 697
Reputation: 597860
Maybe this will make it a little clearer to understand:
typedef WorkerThread* PointerToWorkerThread;
PointerToWorkerThread *workerQueue;
workerQueue = new PointerToWorkerThread[maxThreads];
Upvotes: 2
Reputation: 19052
It's an allocation of an array of WorkerThread
pointers.
For instance:
size_t maxThreads = 3;
WorkerThread** workerQueue = new WorkerThread*[maxThreads];
workerQueue[0]
is a WorkerThread*
, as is WorkerThread[1]
and WorkerThread[2]
.
These pointers, currently have not been initialized.
Later you may see something like this:
for(size_t x = 0; x < maxThreads; ++x)
{
workerQueue[x] = new WorkerThread(...);
//beginthreadex_, CreateThread, something....
}
I will tell you, that all of these raw pointers are just memory leaks waiting to happen unless carefully handled. A preferred method would be to use a std::vector
to some smart pointer of WorkerThread
objects.
Upvotes: 8