Code: Show/Hide #define T_LAYER template <typename T>
T_LAYER class Layer { public: //stuff... void Func1(); void Func2() { printf("hi, this is Func2\n"); } }; T_LAYER void Layer<T>::Func1() { printf("hi, this is Func1\n"); } //meanwhile, in another class not far away... struct HeaderNetwork { char ip[4]; }; class Layer_net : public Layer<HeaderNetwork> { public: //stuff }; //and in the main... int main(void) { Layer_net net; net.Func1(); net.Func2(); } |
Code: Show/Hide #include <stdio.h> #include <stdlib.h> struct HeaderNetwork { char ip[4]; }; template <typename T> class Layer { public: //stuff... void Func1(int); void Func2() { printf("hi, this is Func2\n"); } }; //meanwhile, in another class not far away... class Layer_net : public Layer<HeaderNetwork> { public: //stuff }; |
Code: Show/Hide #include <stdio.h> #include <stdlib.h> #include "header.h" template <typename T> void Layer<T>::Func1(int a) { printf("hi, this is Func1\n"); } |
Code: Show/Hide #include "header.h" //and in the main... int main(void) { Layer_net net; net.Func1(1); net.Func2(); getchar(); } |
Code: Show/Hide hthread = CreateThread(NULL, 0, threadloop, "", 0, &threadID); |
Code: Show/Hide hthread = CreateThread(NULL, 0, &Layer<T>::threadloop, "", 0, &threadID); |
Code: Show/Hide hthread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(&Layer<T>::threadloop), "", 0, &threadID); |
Code: Show/Hide hthread = CreateThread(NULL, 0, &Layer<T>::threadloop, "", 0, &threadID); |
Code: Show/Hide Layer<int> t = new Layer<int>; hthread = CreateThread(NULL, 0, &t::threadloop, "", 0, &threadID); |
Code: Show/Hide T_LAYER
void Layer<T>::Start(void) { if (hthread == NULL) { run_thread = true; hthread = CreateThread(NULL, 0, threadloop, "", 0, &threadID); } } |
Code: Show/Hide void RunThread( T &x, long stackSize = 0) { // each object represents one thread // therefore, should not launch multiple threads with one object assert (NULL == m_threadHandle); // other parameters may be dafaulted if needed DWORD dwStack = (DWORD) stackSize; // create a thread m_threadHandle = CreateThread( NULL, // pointer to thread security attributes dwStack, // initial thread stack size, in bytes StartRun, // pointer to thread function &x, // argument for new thread 0, // creation flags &m_threadId); // pointer to returned thread identifier DWORD last = GetLastError(); // useful for debug stepping if (NULL == m_threadHandle) { throw std::runtime_error("Failed to start thread"); } } static unsigned long __stdcall StartRun(void *param) { T* object = static_cast<T*> (param); // reclaim object type object->Run(); return 0; } |
Samapico wrote: |
I could go the easy way and get rid of the template, that would just give me some copy paste to do |