Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gimp

Coding the Uncreatable

This topic is 6062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!