Topic: Help! on C++ scoping
Author: "Eric Tse" <etse@scdt.intel.com>
Date: 1998/06/13 Raw View
Hi guys,
I have the following classes:
class A { public:
virtual void f() { ... }
};
class B : public A { public:
};
class C : public B { public:
virtual void f() { ...; B::f(); }
};
if someone calls C's f(), will there be a infinite loop? What does
the standard says about it?
Thanks in advance,
Eric
--
--------------------------------------------------------------------
Eric Tse Design Technology, Microprocessor Products Group
Intel Corporation email: etse@scdt.intel.com
M/S RN4-38, 2200 Mission College Blvd. phone: (408)765-8453
Santa Clara, CA 95052 fax: (408)765-5278
--------------------------------------------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: bobh@hablutzel.com (Bob Hablutzel)
Date: 1998/06/14 Raw View
In article <01bd9634$286e6070$cb76b78f@sccia216>, "Eric Tse"
<etse@scdt.intel.com> wrote:
> Hi guys,
>
> I have the following classes:
>
> class A { public:
> virtual void f() { ... }
> };
>
> class B : public A { public:
> };
>
> class C : public B { public:
> virtual void f() { ...; B::f(); }
> };
>
> if someone calls C's f(), will there be a infinite loop? What does
> the standard says about it?
>
> Thanks in advance,
> Eric
Eric -
There will be no infinite loop. The call within C has used a "scope
resolution" which makes sure that the call is treated as if the object
being called were a B, ignoring the fact that it is also a C. In this
case, the A implementation of f will be invoked, as (I think) you expect.
Bob
--
Bob Hablutzel Hablutzel Consulting
bobh@hablutzel.com (603) 431-5074
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1998/06/14 Raw View
>class A { public:
> virtual void f() { ... }
>};
>
>class B : public A { public:
>};
>
>class C : public B { public:
> virtual void f() { ...; B::f(); }
>};
>if someone calls C's f(), will there be a infinite loop? What does
>the standard says about it?
There should be no infinite loop. Here's my take. In the function
C::f() in the call to B::f(), because of the ::, you are turning the
virtual function mechanism off. So B::f() gets called. Because this
function isn't defined by the programmer, it is defined by the
compiler to be the same as A::f() (i.e. the function body and return
type of B::f() is the same as the function body and return type of
A::f()). Thus A::f() gets called.
Regarding your second question, the folks at comp.std.c++ probably
know how to answer this.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]