Original Post
This is my first post on this forum, so please be patient with me...:D I got this working code: /* ctask.h */ /* ccomp.h */ /* ckernel.h */ I'm pretty new working with C++, so I want to ask some questions about this implementation: 1. I found out that I cannot include ctask.h file in ccomp.h file, because ctask.h is already included in ckernel.h therefore the ctask class is already defined when ccomp.h is compiled. From my previous C experience, I know how to implement an #ifdef CTASK_DEFINED mechanism to avoid this double declaration of ctask class, but I want to know if there is another way to avoid this [a cleaner solution without #ifdef]. 2. "(ctask *) new ccomp" is acceptable? 3. What do you think about the way this "ckernel" class is implemented, do you have any suggestions? 4. The coding style is acceptable? Thank you, [Edited by - toro on September 8, 2005 6:26:28 AM]
class ctask
{
public:
ctask();
virtual ~ctask();
virtual void DoSomething();
};
class ccomp: public ctask
{
public:
ccomp();
~ccomp();
void DoSomething();
/*... other methods ...*/
};
void ccomp::DoSomething()
{
/* do something..*/
}
#include <ctask.h>
#include <ccomp.h>
class ckernel
{
public:
ctask *pctask[21];
int task_count;
ckernel();
~ckernel();
void Run();
void AddTask(ctask *ptask);
}
ckernel::ckernel()
{
AddTask((ctask *) new ccomp);
AddTask((ctask *) new ccomp);
}
ckernel::~ckernel()
{
for (int i=0; i<task_count;i++)
{
delete (pctask);
}
}
void ckernel::Run()
{
for (int i=0; i<task_count;i++)
{
pctask->DoSomething();
}
}
void ckernel::AddTask (ctask *ptask)
{
if (task_count < 21)
{
pctask[task_count++] = ptask;
}
else
{
/* do nothing */
}
}