View Full Version : C++ puzzle
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!
anubis
09-07-2004, 04:53 PM
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
Dia Kharrat
09-07-2004, 05:32 PM
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.
anubis
09-07-2004, 07:38 PM
are we two looking at different code ? where is he explictly calling the Base constructor ?
Dia Kharrat
09-07-2004, 08:05 PM
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 */
}
Francois Hamel
09-07-2004, 08:05 PM
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();
SnprBoB86
09-07-2004, 08:11 PM
I think Dia Kharrat 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
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
anubis
09-07-2004, 09:36 PM
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
NomadRock
09-07-2004, 10:18 PM
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.
SnprBoB86
09-07-2004, 10:40 PM
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.
davepermen
09-08-2004, 12:34 AM
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
NomadRock
09-08-2004, 07:43 AM
Yep, microsoft put goto back in.
vBulletin, Copyright ©2000-2009, Jelsoft Enterprises Ltd.