When you realize how dumb a bug is...

Started by
150 comments, last by Juliean 7 years, 5 months ago
Ran across this one in production code 15 minutes ago:

class Base
{
protected:
   virtual ~Base() {}
   virtual void MethodThatDoesAThing() = 0;
};

class Derived : public Base
{
public:
   virtual void MethodThatDoesAThing() override;
};

void Callee(Base* base)
{
   base->MethodThatDoesAThing();
}

void Caller()
{
    static Derived derived;
    Callee(&derived);
}
Surprisingly, VS 2015 compiles this without complaint. I would have expected this to be a compiler error.
Advertisement
^ isn't that of questionable style but prefectly legal? C++ allows overriding your parent's privates.

[edit] Wait... How does Callee not generate an error when it calls a protected function??

[edit2] how are you passing a Derived& to a function that takes a Base*? :P

C++ allows overriding your parent's privates.


With public methods? That seems smelly. In that case, maybe this post should be in the "why didn't somebody tell me" thread.

[edit] Wait... How does Callee not generate an error when it calls a protected function??


In the actual code, Callee is a method on a friend class of Base (but not Derived). Thought it would be simpler to omit that. This isn't code I'd expect anyone to run through a compiler. ;)

[edit2] how are you passing a Derived& to a function that takes a Base*? :P


Whoops. Fixed.

Anyway, upon further reflection, this may not have even been the problem. That would be in line with what I get for jumping to conclusions after only 15 minutes.

C++ allows overriding your parent's privates.

I'm having a hard time reading this with a straight face.
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

I've run into similar problems with some programming projects for school.

i had one project where I had to build a basic ATM and couldn't figure out why the balance for the second person was showing the first person's balance and why the withdraw amount was adding to the balance. I swear it took me longer than it should have to realize that when I duplicated the code for my deposit function and for the second customer I forgot to go in and change the computation signs even though I changed the variable from deposit to withdraw and forgot to change my customer variable form customer1 to customer2. I felt so dumb when I realized it.

This is why I put indenting and // before the quotes and not inside them. (surprised the link stage didn't complain though? is that OK?)

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

I was fighting a bug in the last couple dev sessions I did where my UI would not rasterize my variable, for showing the player, from floats to ints even though I had placed a (int) cast.

the line looked like this (approx.):


hpTextField.text = (int)curHP * modifier + "/" + (int)maxHP * modifier;

With modifier being a float, can you see my error? I didn't... for a week. (working on it on and off)

Spoiler, it was supposed to be (int)(curHP * modifier)

There are 10 types of people in the world: Those who understand binary, those who don't and those who didn't expect this to be in tertiary.

Had a sound engine spazzing out because it turns out the last byte of the stream data got trimmed out somehow. It required three tools and the memory layout to align just right (wrong?) for this to happen.

Why do I keep getting all the weird tool bugs?

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

This topic is closed to new replies.

Advertisement