On 25 Dec 2005 11:58:20 -0800, "gamehack" <gamehack@gmail.com> wrote:
[color=blue]
>Hi all,
>
>I'm designing a few classes and basically I have a base class which is
>very abstract - it has only one method which has to be implemented in
>all others. My question is how to implement it? If I try implementing
>it as
>class ClassName
>{
> public:
> virtual int GetSomeInternalThing();
>};
>
>would that work? Is it okay to leave it without a ctor and a dtor?
>
>Thanks[/color]
You have what looks like an abstract class, in which case the
function(s) meant to be overridden by derived classes need to be
declared pure virtual in the base class.
If you leave out the default constructor and the destructor, the
compiler will generate these (as well as an assignment operator) for
you. Whether or not these are appropriate depend on how you plan to
use the classes. Since it looks like your base class has no data
members, i.e. doesn't manage any resources, this should be OK except
possibly for the virtual destructor which you would need to provide.
For example, if you ever need to delete an instance of a derived class
allocated dynamically through a pointer to the base class, you must
provide a virtual destructor for the base class; otherwise, the
deletion causes undefined behavior.
That would mean something like this:
struct ClassName
{
virtual ~ClassName() {}
virtual int GetSomeInternalThing() const = 0;
};
If GetSomeInternalThing() doesn't change the state of any members in
the derived implementations, it should be declared const as I have
done above. But then ALL of the derived classes must declare it const
in order to override it.
--
Bob Hairgrove
NoSpamPlease@Home.com