| SoftOver |
| Home Humor Puzzles Links Ruby News bLogs Books |
|
Attributed Argument
Puzzles
Please look at the following code and try to find a tiny bug.
#include <iostream>
class SoftwareDeveloper
{
public:
SoftwareDeveloper(unsigned bugPerThousandLines)
: bugsPerThousandLines(bugsPerThousandLines)
{ /*...*/ }
unsigned GetBugs(unsigned linesOfCode)
{
return linesOfCode*bugsPerThousandLines/1000;
}
private:
unsigned bugsPerThousandLines;
};
int main()
{
SoftwareDeveloper JohnDoe(10);
std::cout << JohnDoe.GetBugs(555) << std::endl;
}If you think that initialization with the same name does not work – you are wrong, it works, though it is not trivial to see how it works and why. Try harder, or even try to build it and run. Still puzzled? The bug is here:
SoftwareDeveloper(unsigned bugPerThousandLines)
: bugsPerThousandLines(bugsPerThousandLines)
And it works because of unsigned bugsPerThousandLines; that is just getting initialized with itself.
If the first example was too easy for you – look at the next one. Where is the problem here?
|