Jump to content

  • Log In with Google      Sign In   
  • Create Account






New bug to me

Posted by SiCrane, 13 May 2009 · 175 views

I actually managed to run into a kind of bug in C++ that's new to me.

struct Base {
void print(void) {}
};

struct Derived : Base {
void print(void) {
Base:print();
}
};

int main(int, char **) {
Derived d;
d.print();

return 0;
}

Here Derived::print() wants to call the Base::print() function as part of its implementation. Unfortunately, this doesn't call Base::print(). Base: here is treated as a label before a recursive call to Derived::print() which causes a nice happy stack overflow.

On the plus side, this didn't show up in any actual production code. On the down side, it did show up in the sample exam questions the publisher supplied for a beginner's C++ book. You'd think that for a question along the lines of "what does this code snippet do" that someone would have actually tried compiling and running the code before deciding on an answer.




Classic. One of my favourites is:


class Fun
{
public:
int x;

Fun() {}
};

...

void someFunc()
{
Fun myfun(); // rather than "Fun myfun;"
myfun.x = 12;
}


Which will promptly tell you "left of .x must be a struct/class/union". Guess what? The empty constructor of the stack-allocated variable is treated as a forward declaration of a Fun-returning function with no arguments. This is amusing though, because adding one argument or more to the constructor will suddenly clear up the ambiguity and it will work as expected. [grin]
Quote:
Original post by SiCrane
Here Derived::print() wants to call the Base::print() function as part of its implementation. Unfortunately, this doesn't call Base::print(). Base: here is treated as a label before a recursive call to Derived::print() which causes a nice happy stack overflow.
Oddly, I had the same problem in some code I was writing yesterday:
gaPlayerLocal:;instance().enable(false);

That ended up calling the current classes instance().enable() function instead. It's one of these bugs you can stare at for hours and not see...

*sigh*
Quote:
Original post by HopeDagger
Classic. One of my favourites is:

Which will promptly tell you "left of .x must be a struct/class/union". Guess what? The empty constructor of the stack-allocated variable is treated as a forward declaration of a Fun-returning function with no arguments. This is amusing though, because adding one argument or more to the constructor will suddenly clear up the ambiguity and it will work as expected. [grin]


Love that one. Worse still in a similar vein is:


class c
{
public:
c(float a,float b){ }

void m(){ }
};

void g()
{
int i=10,j=20;

c v(float(i),float(j));
v.m();
}


The compiler mis-interprets the constructor-style casts as function parameters and treats the line as a local prototype.

You've got to love C++. [smile]
Quote:
Original post by SiCrane
... for a beginner's C++ book.

Which one?
Sad thing is, if you mix a lot of Lua programming with your C++ like I do, this kind of typo happens all the time since a single colon is the class method calling convention in Lua.
Nice. Rookie error.

Quote:
Original post by SiCrane
I actually managed to run into a kind of bug in C++ that's new to me.


How is this a 'kind of bug in C++'? As far as i can tell, its doing exactly what you're telling it to. Bug in code != Bug in compiler.

Recent Entries

Recent Comments

PARTNERS