 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Christoph Schulz Guest
|
Posted: Tue Feb 14, 2006 7:06 pm Post subject: Question about the validity of forming and using member poin |
|
|
Hello!
In a comp.lang.c++.moderated thread the following code was deemed illegal:
#include <iostream>
using namespace std;
class Base
{
protected :
virtual void f () {cout << "Base" << endl;}
};
struct Derived2 : Base
{
protected :
virtual void f () {cout << "D2" << endl;}
};
struct Derived : Base
{
void call_f (Base *base)
/*** (1) ***/ {(base->* (&Derived::f)) ();}
};
int main ()
{
Derived d;
Derived2 d2;
d.call_f (&d2);
}
The line (1) was said to engender undefined behavior:
Greg Herlihy wrote:
| Quote: | This code is legal only in the sense that it will compile. The behavior
of its execution however is undefined. It is undefined because the
Derived::call_f() routine applies a member pointer of class Derived to
an object whose dynamic class (Derived2 in this case) contains no
members of class Derived.
§5.5/4 covers this situation:
"If the dynamic type of the object does not contain the member to which
the pointer refers, the behavior is undefined."
|
I'm not sure whether this is true. As I argued, the expression
"&Derived::f" is a member pointer to Base::f because the class Derived
does not directly contain a member named "f" (and the rules of §5.3.1/2
apply). And the dynamic type of the object "base" is Derived2, which
obviously contains Base::f. So I think that the snippet above is legal.
Can anyone clarify this issue?
By the way, the root of the discussion was the question not whether the
program above is legal but whether it should be disallowed, as it could
be seen that the protection of Base::f is undermined. I proposed to
think about a change (and its consequences, naturally) in §5.3.1/2 to
make &Derived::f be of type void(Derived::*)() even if Derived only
inherits the member "f". (As johnchx2 pointed out, there is already a DR
on this (#203).)
Regards,
Christoph
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ] |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Feb 15, 2006 3:16 am Post subject: Re: Question about the validity of forming and using member |
|
|
Christoph Schulz wrote:
| Quote: | Greg Herlihy wrote:
This code is legal only in the sense that it will compile. The behavior
of its execution however is undefined. It is undefined because the
Derived::call_f() routine applies a member pointer of class Derived
|
No, it really doesn't. The trick is to realize that the expression:
&Derived::f
has type void(Base::*)() (i.e. "pointer-to-member-fcn-of-Base taking
no parameter and returning void.")
If reading 5.3.1/2 isn't sufficient, notice that the compiler allows
you to apply the result of &Derived::f to a pointer of type Base*:
void call_f (Base *base)
{
(base->* (&Derived::f)) ();
}
Now try this:
void call_f (Base *base)
{
void (Derived::* pmd)() = &Derived::f; // implicit conversion
(base->* (pmd)) (); // error
}
That is, once you've got an actual pointer-to-member-of-Derived, you
can no longer use it with at Base*.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|