On 2007-12-05 02:27:21 -0500, dolphin <jd*******@gmai l.comsaid:
Hello everyone! Can a static member function access non-static member?
I think it is illegal.Is it right?
Just like any other member function, it has access to private members
of objects of its class. Just like any other member function, it can
access those members through a pointer or reference to an object of its
type. Unlike non-static member functions, it doesn't have an implied
object to work with.
class C
{
public:
void member_func(C *ptr);
static void static_member_f unc(C* ptr);
private:
int i;
};
void C::member_func( C *ptr)
{
ptr->i = 3; // access member of object pointed to by ptr
i = 3; // access member of object pointed to by 'this'
}
void C::static_membe r_func(C *ptr)
{
ptr->i = 3; // access member of object pointed to by ptr
}
--
Pete
Roundhouse Consulting, Ltd. (
www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(
www.petebecker.com/tr1book)