Review code

I'm looking for some advice on an abstract base class that i have created. The class sets up a generic setup for the flow of execution in threads used in an application (init resources, execute and release resources).

Header file:

#ifndef CBASE_H
#define CBASE_H

#include <string>

class CBaseTask
{
public:
CBaseTask();
~CBaseTask();
void Launch(const std::string sName, int iID, bool bRealtime);

protected:
virtual bool Init() = 0;
virtual bool Execute() = 0;
virtual void Terminate() = 0;

private: // methods
bool BaseInit(const std::string sName, int iID, bool bRealtime);
bool BaseExecute();
void BaseTerminate();

private: // variables
std::string m_strName;
bool m_bRealtime;
int m_iID;
};

#endif // CBASE_H

Source file:

#include "cbasetask.h"

CBaseTask::CBaseTask() {}

CBaseTask::~CBaseTask() {}

void CBaseTask::Launch(const std::string sName, int iID, bool bRealtime)
{
if (BaseInit(sName, iID, bRealtime) == true)
{
if (Init() == true) BaseExecute();
}
BaseTerminate();
}

bool CBaseTask::BaseInit(const std::string sName, int iID, bool bRealtime)
{
m_bRealtime = bRealtime;
m_strName = sName;
m_iID = iID;

// allocate base resources
}

bool CBaseTask::BaseExecute()
{
while(true)
{
if (Execute() == false) return false;
// pause (x seconds)
}
return true;
}

void CBaseTask::BaseTerminate()
{
Terminate();
// release base resources
}
[code]
[1972 byte] By [laasunde] at [2007-11-19 22:48:01]
# 1 Re: Review code
I'm not sure what kind of advise you're looking for, but depending on the usage of your abstract class you might consider a virtual destructor.

- petter
wildfrog at 2007-11-9 1:04:03 >
# 2 Re: Review code
Your destructor should be either public and virtual (if you're going to delete it via a pointer to base), or protected and non-virtual (otherwise).

There's no point in making the pure virtual protected - derived classes can't call them (since they have no implementation) and this is the only reason for making a function protected. Make them private.

Don't test for a bool response being equal to true - it makes you look wet behind the ears.

Otherwise, the use of template pattern to force required execution is a good idea.
Graham at 2007-11-9 1:05:08 >
# 3 Re: Review code
Thanks for your valuable tips Graham.

Don't test for a bool response being equal to true - it makes you look wet behind the ears.

I just find using true and false is more explicit than using exclamation mark to evaluate an expression.

if (!foo())
{
// Do stuff
}

if (foo() == false)
{
// Do stuff
}
laasunde at 2007-11-9 1:06:10 >
# 4 Re: Review code
Your decision - but in any code review at any place I've worked, you'd be told to change it.
Graham at 2007-11-9 1:07:12 >
# 5 Re: Review code
There occurs even code likeif(something==true)
return true;
else
return false;
RoboTact at 2007-11-9 1:08:11 >
# 6 Re: Review code
What does OOP say about having data members in abstract base classes?
miteshpandey at 2007-11-9 1:09:16 >
# 7 Re: Review code
I just find using true and false is more explicit than using exclamation mark to evaluate an expression. This will not work if foo() returned an object that has operator ! overloaded.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:10:09 >