Login | Register   
RSS Feed
Download our iPhone app
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: All
May 12, 1998



Application Security Testing: An Integral Part of DevOps

Use derivation instead of type-fields

Whenever your class uses a type-field, as in the following case:
	class Internl { //internationalization aid
		Lang lg; //type field
		FontResource fonts

		enum Lang {English, Hebrew, Danish}
		Internl(Lang lang) lg(lang) {};
		Loadfonts(Lang lang); 
Every modification to the Internl class affects all of its users, even when they should not be affected. For instance, when adding support to a new language, the users of the already-supported languages have to recompile (or worse: download) the new version. Moreover, as time goes by and support for new languages is added, the class becomes bigger, and its code harder to maintain. It takes longer to compile and contains more bugs. A bug introduced due to an addition of a new language will affect all users. Obviously, a much better choice is to use derivation:
	class Internl { //now a base class
		FontResource fonts
		virtual int Loadfonts(); 
		virtual void SetDirectionality();

	class English : public Internl {
		Loadfonts() { fonts = TimesNewRoman; }  
		SetDirectionality(){}//do nothing; default: left to right

	class Hebrew : public Internl {
		Loadfonts() { fonts = David; }  
		SetDirectionality() { directionality = right_to_left;}
The class hierarchy advantages over a type-field are:
  • It simplifies the structure of its classes.
  • It reduces the number of potential bugs (and when they do occur, they are confined to a single class and not shared by all).
  • It improves performance; no use of lengthy switch statements everywhere.
  • Creation of new derived classes does not affect users of existing classes.
  • Danny Kalev
    Comment and Contribute






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



    We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
    Thanks for your registration, follow us on our social networks to keep up-to-date