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
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Feb 2, 1998

WEBINAR:

On-Demand

Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js


Avoid reduplicating identical pieces of contructors' code

There are cases where you have to define several constructors (or constructors and
  operator = 
) within one class, all of which perform some identical tasks (e.g., initialize members, allocate memory, and so on), plus additional individual functionality, as in the following example:
 
class Personnel {
	int children;
	float salary;
	string name;
	...
	public:
	personnel() {	children =0; 
		       	salary = 0.0;
		       	...//individual actions}
	personnel (const string& n) {	children =0; 
		       			salary = 0.0;
						name = n;
		      		 	...}
};
Instead of repeating identical pieces of code in each of the constructors, it
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