Login | Register   
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
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Aug 23, 2005

The 'final' class in C++

Suppose you need to prevent the derivation of a class—like in the final classes of Java. Simply make the constructor of the class private. Here's an example:

class final
{
private:
	final(){} ;

public:
	static final* CreateInstance()
	{
		return (new final()) ;
	}
} ;

/* UNCOMMENTing will give error because we've declared constructor of final class as private
class derived : public final
{
public:
	derived()
	{
	}
} ;
*/

void main()
{
	final *f ;

	f = final::CreateInstance() ;
}
Mitesh Budhabhatti
 
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap