# C++ puzzle

12 replies to this topic

### #1john

Member

• Members
• 84 posts

Posted 07 September 2004 - 10:39 PM

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!

### #2anubis

Senior Member

• Members
• 2225 posts

Posted 07 September 2004 - 10: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
If Prolog is the answer, what is the question ?

### #3Dia

DevMaster Staff

• 1120 posts

Posted 07 September 2004 - 11:32 PM

hehe...nice puzzle. After wasting around 10 min of my time, it seems you're missing a colon 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.

### #4anubis

Senior Member

• Members
• 2225 posts

Posted 08 September 2004 - 01:38 AM

are we two looking at different code ? where is he explictly calling the Base constructor ?
If Prolog is the answer, what is the question ?

### #5Dia

DevMaster Staff

• 1120 posts

Posted 08 September 2004 - 02:05 AM

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 */
}


### #6Francois Hamel

Valued Member

• Members
• 86 posts

Posted 08 September 2004 - 02:05 AM

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();
I post with style, do you?

http://fhamel.blogspot.com/

### #7SnprBoB86

Valued Member

• Members
• 112 posts

Posted 08 September 2004 - 02:11 AM

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
Brandon Bloom
http://brandonbloom.name

### #8john

Member

• Members
• 84 posts

Posted 08 September 2004 - 02:54 AM

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

### #9anubis

Senior Member

• Members
• 2225 posts

Posted 08 September 2004 - 03:36 AM

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
If Prolog is the answer, what is the question ?

Senior Member

• Members
• 785 posts

Posted 08 September 2004 - 04:18 AM

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.
Jesse Coyle

### #11SnprBoB86

Valued Member

• Members
• 112 posts

Posted 08 September 2004 - 04:40 AM

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.
Brandon Bloom
http://brandonbloom.name

### #12davepermen

Senior Member

• Members
• 1306 posts

Posted 08 September 2004 - 06: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
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

Senior Member

• Members
• 785 posts

Posted 08 September 2004 - 01:43 PM

Yep, microsoft put goto back in.
Jesse Coyle

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users