Originally Posted by
Carlos
> for(int i=0;i<5;i++)
> GenerationsNeeded++
1.) Just a hint - there's no reason to use post-increment operator, as far you don't use the temporary returned. It should be:
Code:
for(int i=0;i<5;++i)
...
++GenerationsNeeded;
Well, its just me being used to using post-increment operators. I'll keep that in mind though if i need help with something from here.
Originally Posted by
Carlos
2.) instead of numerical constants, rather use (MACRO) constants to define upper / lower bounds.
Do you mean like declaring a "const int uBound=5"? As that boundary changes with almost every for-loop, i'd much rather have numerical constants on site than a looong list of constants.
Originally Posted by
Carlos
3.) You are passing parameters by value - very bad practice. You should pass them by reference to avoid construction/destruction of temporaries
> Chromosome operator +(Chromosome otherparent)
should be
Code:
Chromosome operator + ( const Chromosome& otherparent )
Ok, I'll fix that but could this cause any errors?
Originally Posted by
Carlos
4.) Much vorse, operator = returns void instead of reference to this. And again, parameter passing...
> void operator =(Chromosome equality)
Is it possible to have
Code:
Chromosome a,b,c;
a = b = c;
and it is the reason for having = return a reference to an object?
Originally Posted by
Carlos
I'm stopping now, you should inspect a good C++ book than revise your code.
I've gone through 6 books used by the swedish school with a magnifying glass, granted that they were less-than-good at times... Can you recommend any books?
Originally Posted by
Carlos
Keybone, if you're not using MS VisualC++, you should place try / catch( bad_alloc& ) pairs where new operator is called, for VC++ checking for NULL pointers will do it.
I am using DJGPP, so I'll try that.There are no explicit new's in the source but Im guessing I should try/catch where I create the Population array like below?
Code:
int main()
{
srand(time(NULL));
try{
Chromosome Population[101];
}
catch(bad_alloc)
{
cerr << "Bad Allocation";
exit(1);
}
...
The source has been edited in the first post.