Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 25th, 2005, 08:05 PM
gamehack
Guest
 
Posts: n/a
Default Class Design Question

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

  #2  
Old December 25th, 2005, 08:35 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Class Design Question

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
  #3  
Old December 25th, 2005, 08:45 PM
Peteris Krumins
Guest
 
Posts: n/a
Default Re: Class Design Question

gamehack 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?
>[/color]

This is not an abstract. A class with one or more pure virtual funcions
is called an abstract class, and no objects of abstract class can be
created. An abstract class can be
used only as an interface and as a base class for other classes.

Make it like:

class ClassName
{
public:
virtual int getSomeIntegralThing() = 0;
};

class OneHundred : public ClassName
{
public:
virtual int getSomeIntegralThing() { static int i = 100; return i++; }
};

class SuperEnumerator : public ClassName
{
int start_;
public:
SuperEnumerator(int start) : start_(start) { }

virtual int getSomeIntegralThing() { return start_++; }
};

int main()
{
OneHundred oh;
SuperEnumerator se(-20);
int id1, id2;

id1 = oh.getSomeIntegralThing();
id2 = se.getSomeIntegralThing();
}

etc.

If you do not explicitly define your own constructor, the compiler will
generate a default constructor for you. A default constructor is a
constructor that can be called without supplying an argument.

If you do not explicitly define your own destructor then the compiler
generates one which does nothing.

So it's ok to leave a class without a ctor and dtor.


P.Krumins

  #4  
Old December 25th, 2005, 09:05 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Class Design Question

On 25 Dec 2005 12:37:34 -0800, "Peteris Krumins"
<peteris.krumins@gmail.com> wrote:
[color=blue]
>So it's ok to leave a class without a ctor and dtor.[/color]

Unless, of course, an object of the derived type is deleted through a
pointer to the base class. In that case, you must provide the base
class with a virtual destructor; otherwise, the behavior is undefined.

--
Bob Hairgrove
NoSpamPlease@Home.com
  #5  
Old December 25th, 2005, 09:25 PM
gamehack
Guest
 
Posts: n/a
Default Re: Class Design Question

Thanks a lot guys :)
BTW I've always wondered
virtual int GetSomeInternalThing() const = 0;
what do the keywords after () mean and the assignment in the context?

Thanks

  #6  
Old December 25th, 2005, 09:35 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Class Design Question

On 25 Dec 2005 13:16:48 -0800, "gamehack" <gamehack@gmail.com> wrote:
[color=blue]
>Thanks a lot guys :)
>BTW I've always wondered
>virtual int GetSomeInternalThing() const = 0;
>what do the keywords after () mean and the assignment in the context?[/color]

Have you read this?
http://www.parashift.com/c++-faq-lite/

--
Bob Hairgrove
NoSpamPlease@Home.com
  #7  
Old December 25th, 2005, 09:35 PM
Mateusz Łoskot
Guest
 
Posts: n/a
Default Re: Class Design Question

gamehack wrote:[color=blue]
> Thanks a lot guys :)
> BTW I've always wondered
> virtual int GetSomeInternalThing() const = 0;
> what do the keywords after () mean and the assignment in the context?[/color]

http://parashift.com/c++-faq-lite/co...html#faq-18.10

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
  #8  
Old December 25th, 2005, 10:25 PM
Peteris Krumins
Guest
 
Posts: n/a
Default Re: Class Design Question


Bob Hairgrove wrote:[color=blue]
> On 25 Dec 2005 12:37:34 -0800, "Peteris Krumins"
> <peteris.krumins@gmail.com> wrote:
>[color=green]
> >So it's ok to leave a class without a ctor and dtor.[/color]
>
> Unless, of course, an object of the derived type is deleted through a
> pointer to the base class. In that case, you must provide the base
> class with a virtual destructor; otherwise, the behavior is undefined.
>[/color]

Thanks for noting that!


P.Krumins

  #9  
Old December 25th, 2005, 10:25 PM
gamehack
Guest
 
Posts: n/a
Default Re: Class Design Question

Right :) After I googled a bit I found about pure virtual functions
which make the class abstract. That was exactly what I was looking
about. Thanks.

  #10  
Old December 26th, 2005, 09:15 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: Class Design Question


"gamehack" <gamehack@gmail.com> wrote in message
news:1135545408.324832.301230@o13g2000cwo.googlegr oups.com...[color=blue]
> Thanks a lot guys :)
> BTW I've always wondered
> virtual int GetSomeInternalThing() const = 0;
> what do the keywords after () mean and the assignment in the context?
>
> Thanks[/color]

The const means that this method can't change anything in the class. the =
0 means that it's a pure virtual method, so derived classes must provide
their own or they won't compile.


  #11  
Old December 26th, 2005, 04:45 PM
Mateusz oskot
Guest
 
Posts: n/a
Default Re: Class Design Question

Jim Langston wrote:[color=blue]
> "gamehack" <gamehack@gmail.com> wrote in message
> news:1135545408.324832.301230@o13g2000cwo.googlegr oups.com...
>[color=green]
>>Thanks a lot guys :)
>>BTW I've always wondered
>>virtual int GetSomeInternalThing() const = 0;
>>what do the keywords after () mean and the assignment in the context?
>>
>>Thanks[/color]
>
>
> The const means that this method can't change anything in the class.[/color]

Not in the class, but the object.
Method declared as const has "read-only" access to the object itself,
because inside method declared as const "this" pointer is a const
pointer to const object (MyClass const* const).
Cheers
--
Mateusz oskot
http://mateusz.loskot.net
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles