tags:

views:

353

answers:

4

A friend of mine sent me this code snippet to celebrate his new baby birth:

void new_baby_name() { father_surname++; }

The snippet is from his point of view, he is the father and the new baby get the surname from him.

I answered with this:

class father_name {};
class mother_name {};
class new_baby_name: public father_name, public mother_name {};

but I am not fully satisfied of my answer...

+6  A: 
class baby
{
public:
    vector<gene> genes;
    baby(baby* logical_father, baby* biological_mother, baby* other)
    {

        int i;
        if (other == null)
        {
            for (i = 0; i < logical_father->genes.size())
            {
                if (rand() > 0.5)
                {
                    genes.push_back(logical_father->genes[i]);
                }
                else
                {
                    genes.push_back(biological_mother->genes[i]);
                }
            }
        }
        else
        {
            for (i = 0; i < other->genes.size())
            {
                if (rand() > 0.5)
                {
                    genes.push_back(other->genes[i]);
                }
                else
                {
                    genes.push_back(biological_mother->genes[i]);
                }
            }
        }
    }
}

There are, of course, other methods for constructing a baby.

Simon
Mother and father are both `baby`'s o_O
KennyTM
It would be simpler if you started off by determining who the father is, but nooo…
Potatoswatter
@Simon: Genes can only be inherited from a biological father, not from a logical father.
Manav MN
Ugh, what an ugly style with all these needles braces - and a needlessly function-scoped `int`, too. Put that code into a function with less than half a dozen lines and the ctor becomes a one-liner. Oh, and with proper identifier names (`biological_father`) Manav (and - so far - three others who up-voted his comment) wouldn't have misunderstood `other`.
sbi
+1 very good! @sbi there's nothing wrong with this code.
Ninefingers
@sbi, I didn't realise you had been appointed code style Police. When did that happen? As it goes, I prefer it like that, there's nothing syntactically wrong with it and there's nothing good about a 1 line constructor. Furthermore you are showing your lack of subtelty in questioning the variable names. Look at it again, and think about the real world.
Simon
@Manav MN. That's true, but it presumes you know which of the logical father and other is the biological father.@KennyTM, we were all babies once!
Simon
@Simon: I'm your next-door plain programmer. That's why I can express my feelings the way I feel them. Had I been appointed police, I would have to be more diplomatic. `:) `
sbi
Would be better with `baby *father = other ? other : logical_father` and only one loop (using `father`). Not to mention const references over pointers.
asveikau
A: 
destroy Sanity();

May not run, may stack overflow. I am not good at c.

glasnt
That would actually be `~Sanity ();` which call's Sanity's destructor.
Joe D
A: 

Just a small bugfix to avoid bad names:

class male {};
class female {};

class father_name {};
class mother_name {};

template <class gender>
class new_baby_name;

template <>
class new_baby_name<male>: public father_name {};

template <>
class new_baby_name<female>: public mother_name {};

Note that you have a serious problem if this should fire a compiler error ;-)

Alexander Gessler
Why inherit from compiletime_error, surely just declaring `class new_baby_name;` and not defining it would work?
Joe D
+23  A: 

The correct reply is:

Sleep(0);
Jason Williams