Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Jun 30, 2000

Abstract Base Class(ABC) Creation

Question:
How do I convert a class into an abstract base class without using a pure virtual member function in it?

Answer:
If your goal is to disable instantiation of a given class while allowing instantiation of classes that are derived from it, you can declare the class's constructor, destructor, copy constructor, and assignment operator protected. This way, derived classes, which define these members public by default, can be instantiated whereas objects of the "abstract" class cannot:

class Abs
{
protected:
 Abs();
 virtual ~Abs();
 //...etc.
};

Abs a; //compilation error; ctor inaccessible

Class Derived: public Abs
{};

Derived d; //OK
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date