On Dec 5, 9:27 am, dolphin <jdxyw2...@gmail.com> wrote:
> Hello everyone! Can a static member function access non-static member?
> I think it is illegal.Is it right?
Write a 10-line test program and see
This is a discussion on Can a static member function access non-static member? - c++ ; Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?...
Hello everyone! Can a static member function access non-static member?
I think it is illegal.Is it right?
On Dec 5, 9:27 am, dolphin <jdxyw2...@gmail.com> wrote:
> Hello everyone! Can a static member function access non-static member?
> I think it is illegal.Is it right?
Write a 10-line test program and see
dolphin wrote:
> Hello everyone! Can a static member function access non-static member?
> I think it is illegal.Is it right?
Yes, provided it has a pointer or reference to the object. Why
shouldn't it?
--
Ian Collins.
On 2007-12-05 02:27:21 -0500, dolphin <jdxyw2004@gmail.com> said:
> 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_func(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_member_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)