Coding the Uncreatable

Started by
18 comments, last by gimp 22 years, 4 months ago
I''d like to code a class CNonCreatable. So, that when I create a new class, say CPartition, which shouldn''t be used for anything but deriving from. How can I do that cleanly? Chris Brodie http:\\fourth.flipcode.com
Chris Brodie
Advertisement
in "java" make the class "abstract".
in "c++" just have one method that is abstract:

i.e.

class CNonCreatable {
public:
virtual SomeMethod(void) = 0;
};

// the "= 0;" makes the method abstract and thus cannot create an instance of this class. not sure if you need the virtual.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Try this:
  class CNonCreatable {	public:		virtual ~CNonCreatable(void) = 0;};CNonCreatable::~CNonCreatable(void) { }class CPartition : public CNonCreatable {	public:		int a;};  

I''ve never done this before, but as far as I know it should work .

[Resist Windows XP''s Invasive Production Activation Technology!]
It works. Those "=0" functions are called pure virtual methods (in C++), and you are not permitted to create instances of pure virtual classes (classes with one or more pure virtual methods).

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Oluseyi: that''s right, i just couldn''t remember the name even though i use it for my base class "Object" for everything else i do.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
quote:Original post by Oluseyi
It works. Those "=0" functions are called pure virtual methods (in C++), and you are not permitted to create instances of pure virtual classes (classes with one or more pure virtual methods).


..., and you have to define in your derived class the method defined as pure virtual (int doIt()=0; ) in your super class.


Edited by - Floppy on December 12, 2001 8:08:23 PM
quote:Original post by Floppy
..., and you have to define in your derived class the method defined as pure virtual (int doIt()=0; ) in your super class.

Yeah, thanks for that.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Alternatively, you can just make your constructor protected.

xyzzy
quote:Original post by xyzzy00
Alternatively, you can just make your constructor protected.


That''s quite close to the Singleton pattern, where the private/protected constructor is complemented by a Create() method. If your Create() method indiscriminately returns valid objects, then making the constructor protected is a waste.

However, while a protected constructor will not allow objects of that class to be created, it doesn''t ensure that an instance never exists. That''s specifically what pure virtual functions are for; why not use them?

*shrugs*

No big deal I guess - another preference.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
And you don''t even have to make every derived class implement the dummy function.

this is valid:

class CNonCreate
{
protected:
virtual void dummyfunc() = 0;
};

void CNonCreate::dummyfunc()
{}

class CCreate : public CNonCreate
{
using CNonCreate::dummyfunc;
public:
};

this way you don''t have to implement the dummyfunc in the derived classes
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement