Topic: Pointers to Members
Author: jason@cygnus.com (Jason Merrill)
Date: 1995/04/11 Raw View
>>>>> Andy Sawyer <andys@thone.demon.co.uk> writes:
> int *foo1() { return &Base1::x; }
> int *foo2() { return &Base2::x; }
> // Now the ptr to member versions....
> int Derived::* bar1() { return &Base1::x; }
> int Derived::* bar2() { return &Base2::x; }
> // Oh dear?does &Base::x mean &(my_Base1_x) or (ptr-to-x-in-Base1???)
It is a pointer to member; &(Base::x) gives you a pointer to int. Or at
least that's how I read this passage:
Jason
5.3.1 Unary operators [expr.unary.op]
2 The result of the unary & operator is a pointer to its operand. The
operand shall be an lvalue or a qualified-id. In the first case, if
the type of the expression is T, the type of the result is pointer to
T. In particular, the address of an object of type cv T is pointer to
cv T, with the same cv-qualifiers. For example, the address of an
object of type const int has type pointer to const int. For a quali-
fied-id, if the member is a nonstatic member of class C of type T, the
type of the result is pointer to member of class C of type T. For
example:
struct A { int i; };
struct B : A { };
... &B::i ... // has type "int A::*"
For a static member of type T, the type is plain pointer to T. Note
that a pointer to member is only formed when an explicit & is used and
its operand is a qualified-id not enclosed in parentheses. For exam-
ple, the expression &(qualified-id), where the qualified-id is
enclosed in parentheses, does not form an expression of type pointer
to member. Neither does qualified-id, and there is no implicit con-
version from the type nonstatic member function to the type pointer to
member function, as there is from an lvalue of function type to the
type pointer to function (_conv.func_). Nor is &unqualified-id a
pointer to member, even within the scope of unqualified-id's class.
Author: andys@thone.demon.co.uk (Andy Sawyer)
Date: 1995/04/11 Raw View
In article <JASON.95Apr10231214@phydeaux.cygnus.com>
jason@cygnus.com "Jason Merrill" writes:
> >>>>> Andy Sawyer <andys@thone.demon.co.uk> writes:
>
> > int *foo1() { return &Base1::x; }
> > int *foo2() { return &Base2::x; }
> > // Now the ptr to member versions....
> > int Derived::* bar1() { return &Base1::x; }
> > int Derived::* bar2() { return &Base2::x; }
> > // Oh dear?does &Base::x mean &(my_Base1_x) or (ptr-to-x-in-Base1???)
>
> It is a pointer to member; &(Base::x) gives you a pointer to int. Or at
> least that's how I read this passage:
>
> Jason
>
> 5.3.1 Unary operators [expr.unary.op]
>
[snip]
Thanks! I've been trying to figure this one for a while now. I knew there
had to be something I'd missed (a decent compiler, perhaps :-()
Regads,
Andy
--
* Andy Sawyer ** e-mail:andys@thone.demon.co.uk ** Compu$erve:100432,1713 **
The opinions expressed above are my own, but you are granted the right to
use and freely distribute them. I accept no responsibility for any injury,
harm or damage arising from their use. -- The Management.
Author: andys@thone.demon.co.uk (Andy Sawyer)
Date: 1995/04/10 Raw View
Hi All.
I have found a problem with pointers to members, and thought it may be
worth discussing here:
How do you get a pointer-to-member pointer in a member function?
Example:
class Base {
int x;
public:
int * foo() { return &x; }
int Base::* bar() { return &Base::x; }
};
This looks like it ought to work at first glance, but what about this?
class Base1 {
protected:
int x;
};
class Base2 {
protected:
int x;
};
class Derived : public Base1, public Base2 {
public:
int *foo() { return &x; } // Oh dear, this is ambiguous...
// soo...need to specify WHICH x...
int *foo1() { return &Base1::x; }
int *foo2() { return &Base2::x; }
// Now the ptr to member versions....
int Derived::* bar1() { return &Base1::x; }
int Derived::* bar2() { return &Base2::x; }
// Oh dear?does &Base::x mean &(my_Base1_x) or (ptr-to-x-in-Base1???)
};
Ideas, anyone?
Best Wishes,
Andy
--
* Andy Sawyer ** e-mail:andys@thone.demon.co.uk ** Compu$erve:100432,1713 **
The opinions expressed above are my own, but you are granted the right to
use and freely distribute them. I accept no responsibility for any injury,
harm or damage arising from their use. -- The Management.