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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: Intermediate
Jul 10, 1998

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


When can an empty class be useful?

A class containing no data members and no member functions is an empty class. It's defined like this:
 
class PlaceHolder {}; 
What's it good for? It can serve as a place-holder for a yet-to-be defined class. For instance, it can be used as an interface class serving as a base for other classes; instead of waiting for it's full implementation to be completed, it can be used this way in the interim. An empty class can also be used as a means of forcing derivation relationship among classes which are not originally descended from one base class. This is called a bottom-up design. Finally, it can be used to create a type for a dummy argument to distinguish between overloaded version of a function. In fact, operator new is overloaded exactly like that:
 
#include 
using namespace std;
void main()
{
try {
int *p = new int[100]; //standard exception-throwing version of new 
}

catch(bad_alloc & new_failure) {/*..*/}

int *p = new (nothrow) int [100]; //standard exception-free version of new; returns NULL if fails
if (p) {/*..*/}
}
The nothrow arguments is of type nothrow_t, which is an empty class by itself.
Danny Kalev
 
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