[class constructor] function foo to make _job job#.name where # is foo parameter

Oct 12, 2009 at 5:56pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int nDqJobIndex = 0;
void dqJobNew(int &nDqJobIndex, string szName, string szTitle, int nSalary, int nCreditYearsMin)
{
	_job job"nDqJobInex";//???
	//(...)
	nDqJobIndex++;
}

dqJobNew(nDqJobIndex, "Unemployed", "WELFARE RECIPIENT", 5000, 0);

/*
_job job0;
job0.name = "Unemployed";
job0.title = "WELFARE RECIPIENT";
job0.salary = 5000;
job0.creditYearsMin = 0;
*/


I don't think the nDqJobIndex incrementing thing will be a problem, and if it is I'll make a different thread. My issue is that I don't know how to handle these lines:
1
2
	_job job"nDqJobInex";//???
	//(...) 

Last edited on Oct 12, 2009 at 10:43pm
Oct 12, 2009 at 6:19pm
Use an array or an std::vector.
Oct 12, 2009 at 6:22pm
You can't make symbols runtime, if this is what you are trying to do
Oct 12, 2009 at 6:24pm
firedraco, good point, and i already have the array. i'll kill those two birds next. this is what i've got (which works) for now:

1
2
3
4
5
6
7
void dqJobNew(_job &job, string szName, string szTitle, int nSalary, int nCreditYearsMin)
{
	job.name = szName;
	job.title = szTitle;
	job.salary = nSalary;
	job.creditYearsMin = nCreditYearsMin;
}


1
2
3
_job job0, job1;
dqJobNew(job0, "Unemployed", "WELFARE RECIPIENT", 5000, 0);
dqJobNew(job1, "Labor Ready", "LABORER", 12500, 0);

Oct 12, 2009 at 9:37pm
Why are you not just making a constructor for _job that takes the name, title, salary,
and credit years min as parameters? It is so much cleaner than that function.
Oct 12, 2009 at 10:25pm
Last edited on Oct 12, 2009 at 11:21pm
Oct 12, 2009 at 11:21pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class _job
{
public:
	string name, title;
	int salary, creditYearsMin, nIqEffect;
	_job()
	{
	}
	_job(string szName, string szTitle, int nSalary, int nCreditYearsMin, int nIqEffect)
	{
	name = szName;
	title = szTitle;
	salary = nSalary;
	creditYearsMin = nCreditYearsMin;
	nIqEffect = nIqEffect;
	}
};


1
2
3
4
5
6
deque<_job> dqJob(25);

dqJob[0] = _job("dqJob[0].name", "dqJob[0].title", 0, 0, 0);
dqJob[1] = _job("Unemployed", "WELFARE RECIPIENT", 5000, 0, -4);
dqJob[2] = _job("Labor Ready", "LABORER", 12500, 0, -1);
(...)


Works like a charm. Thanks, jsmith!
Oct 13, 2009 at 12:55pm
Great, though your default constructor should really initialize the integers to zero (the strings are already
being default constructed to an empty string).

You should also use an initializer list (in both constructors):

1
2
3
4
_job( const std::string& name, const std::string& title, int salary, int creditYears, int iqEffect ) :
   name( name ), title( title ), salary( salary ), creditYearsMin( creditYears ), nIqEffect( iqEffect )
{
}


With an initializer list your code is slightly more efficient.
Oct 13, 2009 at 11:41pm
Ah, thank you.

Old code:
1
2
3
4
5
6
7
8
_job(string szName, string szTitle, int nSalary, int nCreditYearsMin, int nIqEff)
	{
	name = szName;
	title = szTitle;
	salary = nSalary;
	creditYearsMin = nCreditYearsMin;
	nIqEffect = nIqEff;
	}


New code:
1
2
3
4
	_job( const std::string& szName, const std::string& szTitle, int nSalary, int nCreditYearsMin, int nIqEff ) :
   name( szName ), title( szTitle ), salary( nSalary ), creditYearsMin( nCreditYearsMin ), nIqEffect( nIqEff )
	{
	}
Topic archived. No new replies allowed.