C++ puzzle

3c6597370b476903ed475f70b4b3ce31
0
john 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

Please log in or register to post a reply.

F7a4a748ecf664f189bb704a660b3573
0
anubis 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

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 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.

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 08, 2004 at 01:38

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

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 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 */
  }
218341be2587d9bdef38af0c2066c308
0
Francois_Hamel 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();

6e0b32504d31ae07efc17f3322cdb147
0
SnprBoB86 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

3c6597370b476903ed475f70b4b3ce31
0
john 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

F7a4a748ecf664f189bb704a660b3573
0
anubis 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

7543b5c50738e23b200e69fe697ea85a
0
NomadRock 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.

6e0b32504d31ae07efc17f3322cdb147
0
SnprBoB86 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.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 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

7543b5c50738e23b200e69fe697ea85a
0
NomadRock 101 Sep 08, 2004 at 13:43

Yep, microsoft put goto back in.