Topic: function calls using pointer to member function
Author: "Larry Brasfield" <larrybr@seanet.com>
Date: 1999/02/03 Raw View
Francis Glassborow wrote in message ...
>
>In article <36B7DCC3.B17DD259@mindless.com>, Marcin Bilski
><bilus@mindless.com> writes
>>> Can someone tell me (by quoting the appropriate clauses in the standard)
>>> why the first call to foo calls A::foo but the second one calls
>>> B::foo?
>>
>>Something wrong with your compiler. MSVC++ 6.0 is happy with polymorphism in
>>either case.
As I explained, and as can be seen by referring to
section 5.2.2, paragraph 1 of the standard, which I
cited, the behavior claimed by sanjayp@ddi.com
is required by the C++ standard. In response to
Mr. Bilski's post, I personally compiled and ran
the code from the original post with MSVC++ 6.0
and verified that it runs correctly. (virtual call for
pointer-to-member dereference and non-virtual
call for qualified-id call) So, I can confidently say
that Mr. Bilski's claim that another compiler has
(that) something wrong is nonsense and that his
claim regarding MSVC++ 6.0 is false for the most
sensible relevant interpretation of "is happy with
polymorphism in either case." (I take those words
to mean the virtual call mechanism is effective in
both of the presented calls.)
>Really? Then I think there is something wrong with your compiler
>(MSVC++ 6.0)
Not really. That was just ill-considered noise.
--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.com )
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/02/04 Raw View
In article <Tv1u2.2074$GN.865@news.rdc1.wa.home.com>, Larry Brasfield
<larrybr@seanet.com> writes
>>Really? Then I think there is something wrong with your compiler
>>(MSVC++ 6.0)
>
>Not really. That was just ill-considered noise.
FWIW, the question mark after really indicates doubt as to the truth of
the claim. Conditional on it being true then there was something wrong
with his compiler.
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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: sanjayp@ddi.com
Date: 1999/02/03 Raw View
I have a small program
extern "C" int printf(const char *,...);
class A {
public:
virtual int foo(int i)
{
return i;
}
};
class B : public A {
public:
int foo(int i)
{
return i + 1;
}
};
int main()
{
B b;
A *a = &b;
printf("%d\n",(a->A::foo)(3));
printf("%d\n",(a->*(&A::foo))(3));
return 0;
}
Can someone tell me (by quoting the appropriate clauses in the standard)
why the first call to foo calls A::foo but the second one calls
B::foo?
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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: "Larry Brasfield" <larrybr@seanet.com>
Date: 1999/02/03 Raw View
sanjayp@ddi.com wrote in message <79837s$3j8$1@nnrp1.dejanews.com>...
>
>I have a small program
>
>extern "C" int printf(const char *,...);
That declaration looks like bad news. I suppose
it happens to match what your library provides,
but that is not a supposition anybody should
have to make.
>class A {
>public:
> virtual int foo(int i)
> {
> return i;
> }
>};
>
>class B : public A {
>public:
> int foo(int i)
> {
> return i + 1;
> }
>};
>
>int main()
>{
> B b;
> A *a = &b;
>
> printf("%d\n",(a->A::foo)(3));
Ok, a specific member of A has been specified,
bypassing the virtual mechanism altogether. That
is the effect of using the scope resolution operator
to directly specify what is to be called.
> printf("%d\n",(a->*(&A::foo))(3));
Here, a pointer-to-member is created and then
dereferenced, so the virtual call mechanism
takes effect because that is the behavior
defined for pointer-to-member.
> return 0;
>}
>
>Can someone tell me (by quoting the appropriate clauses in the standard)
>why the first call to foo calls A::foo but the second one calls
>B::foo?
I can, but I won't. In "The C++ Programming Language" (3rd Ed.),
at section 15.5, Stroustrup describes pointers to members as
behaving the way you've seen and I've claimed above. I doubt
the effect of the scope resolution operator needs elaboration.
This is more or less spelled out in section 5.2.2, paragraph 1
of the standard, which specifically mentions the "qualified-id"
exception (use of scope resolution operator) to the virtual
call mechanism that otherwise applies to virtual functions.
The language there covers the pointer-to-member case.
--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.com )
---
[ 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: Marcin Bilski <bilus@mindless.com>
Date: 1999/02/03 Raw View
sanjayp@ddi.com wrote:
> I have a small program
<snip>
> Can someone tell me (by quoting the appropriate clauses in the standard)
> why the first call to foo calls A::foo but the second one calls
> B::foo?
Something wrong with your compiler. MSVC++ 6.0 is happy with polymorphism in
either case.
Bilus
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/02/03 Raw View
In article <36B7DCC3.B17DD259@mindless.com>, Marcin Bilski
<bilus@mindless.com> writes
>> Can someone tell me (by quoting the appropriate clauses in the standard)
>> why the first call to foo calls A::foo but the second one calls
>> B::foo?
>
>Something wrong with your compiler. MSVC++ 6.0 is happy with polymorphism in
>either case.
Really? Then I think there is something wrong with your compiler
(MSVC++ 6.0)
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/02/03 Raw View
In article <eBOt2.2051$GN.872@news.rdc1.wa.home.com>, Larry Brasfield
<larrybr@seanet.com> writes
>>extern "C" int printf(const char *,...);
>
>That declaration looks like bad news. I suppose
>it happens to match what your library provides,
>but that is not a supposition anybody should
>have to make.
If I remember correctly, this is one of the changes from C to C++. C
allows explicit declaration of library functions (as long as you get
them right:) C++ prohibits it. If you want a library function you must
include the relevant header.
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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 ]