New bug to me

Published May 13, 2009
Advertisement
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.
Previous Entry Week/Class 9
0 likes 6 comments

Comments

HopeDagger
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]
May 13, 2009 10:02 PM
Evil Steve
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*
May 14, 2009 03:46 AM
Aardvajk
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]
May 14, 2009 11:57 AM
Gaiiden
Quote:Original post by SiCrane
... for a beginner's C++ book.

Which one?
May 16, 2009 08:30 PM
JTippetts
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.
May 19, 2009 07:34 AM
thefries
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.
June 30, 2009 08:48 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

New bug to me

1678 views

Week/Class 9

1517 views

Week/Class 8

1555 views

Week/Class 7

1605 views

The promised files

1834 views

Week/Class 6

1300 views

Week/Session 5

1355 views

Week/Session 4

1294 views

On the soapbox

1396 views

Week/Session 3

1271 views
Advertisement