0
102 Sep 07, 2004 at 22:39

The following code causes an infinite loop. Can you spot the problem and explain why?

class Base
{
public:
Base() {}
virtual void func() { /* do something */ }
};

class Derived : public Base
{
public:
Derived() {}
virtual void func()
{
Base:func();
/* do something else */
}
};

int main()
{
Derived d;
d.func();  // Never returns!
}


Let’s see if someone can spot the problem!

#### 12 Replies

0
101 Sep 07, 2004 at 22:53

i’m tired so i may be wrong but there is no infinite loop… if you call Base::func() from within Derived::func() it doesn’t matter wether Base::func is virtual or not. the base classes func() will be called.

class Base
{
public:

virtual void func() { ... }
void func2() { func(); }
}

class Derived : public Base
{
void func() { Base::func2(); }
}


this would create an infinite loop

0
157 Sep 07, 2004 at 23:32

hehe…nice puzzle. After wasting around 10 min of my time, it seems you’re missing a colon :D when calling the base contructor, which causes the function to be called recursively. However, practically speaking, this won’t be an infinite loop and will rather cause a stack overflow.

0
101 Sep 08, 2004 at 01:38

are we two looking at different code ? where is he explictly calling the Base constructor ?

0
157 Sep 08, 2004 at 02:05

I was looking at this part of the code:

  virtual void func()
{
Base:func();  // <--- here
/* do something else */
}


“Base:” acts like a simple label. So it can be rewritten as:

  virtual void func()
{
Base:
func();
/* do something else */
}

0
101 Sep 08, 2004 at 02:05

he meant the base method maybe…

this:

Base:func();

is interpreted as a LABEL followed by a function call to the member function func()…so it causes an infinit recursion.

the real thing would have been

Base::func();

0
101 Sep 08, 2004 at 02:11

I think Dia is the winner…

<voice volumn=”low” tone=”lieing”>
I saw that right away, but I just didn’t want to say it because it would have made you all feel bad about yourselves…
</voice>

:D

0
102 Sep 08, 2004 at 02:54

ya, Dia wins. You guys like these puzzles? should I post more? Maybe we could have something like “The Puzzle of the Day” :lol:
anyways…Dia: sorry for wasting your time :D

0
101 Sep 08, 2004 at 03:36

obvious… simply overread it :D well… congrats to dia

yeah… feel free to post more
i’ll be on the watch for the next one

0
101 Sep 08, 2004 at 04:18

I caught the single colon where it should have been a double colon, but I thought that was your typo and not what the code was supposed to be about. As anubis, I began to overanalyze it.

To be honest though, I had forgotten that this was even valid syntax. Curse the day goto was allowed to continue. Java did a great thing by reserving it as a keyword and not implementing it. Curse C# for bringing it back!

At any rate, I did enjoy this, and I will be on the lookout for more trixy work from you.

0
101 Sep 08, 2004 at 04:40

C# has a goto?

Woa, coulda fooled me. I have been coding advidly in C# for two years (maybe more) and can’t remember a single mention of it in any material I have read.

It must have been mentioned in my C# book, I intentionally forget things like this.

0
101 Sep 08, 2004 at 06:34

c# has a goto? not only as a reserved word, but for use? never seen it, nor used it.. ugly..

oh, and.. yeah, c++ is great for allowing typos to simply be a different syntax :D this was just one such example

0
101 Sep 08, 2004 at 13:43

Yep, microsoft put goto back in.