473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can a static member function access non-static member?

Hello everyone! Can a static member function access non-static member?
I think it is illegal.Is it right?
Dec 5 '07 #1
3 5605
On Dec 5, 9:27 am, dolphin <jdxyw2...@gmail.comwrote:
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 :)
Dec 5 '07 #2
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.
Dec 5 '07 #3
On 2007-12-05 02:27:21 -0500, dolphin <jd*******@gmail.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_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)

Dec 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
6
by: Ryan Muller | last post by:
My company just upgraded from Access 97 to Access 2003 today and we are having some issues in a database that generates a Word document from information selected in a form. Here is the code we...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
4
by: gs | last post by:
I have searched Google, MSDN,... for a week. I am still unable to make available functions in my csharp dll as native windows functions for some legacy non dotnet application I just want to...
9
by: Roshni | last post by:
Hi, I wanted to know how do function pointers sometime access illegal memory access ? Could any one give me an example ? Thanks, Roshni
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
3
by: ManWithNoName | last post by:
Hi! I am trying to load files that contain non-standard (and non-English) characters (e.g. µ), and need some help in understanding how the XSLT document() function interprets non-standard (and...
8
Cintury
by: Cintury | last post by:
The problem is I have a function that I've created and stored in a module. I call it as an expression (e.g. total: Function(parameter)). I'm receiving the error 3061: too few parameters, expected 1....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.