473,569 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5623
On Dec 5, 9:27 am, dolphin <jdxyw2...@gmai l.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*******@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)

Dec 5 '07 #4

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

Similar topics

9
4940
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or...
19
2774
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 version of that type. Is this a bug that is specific to gcc, or is it a flaw in the language specification that gcc diligently implements? For...
6
2670
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 were using without issue previous to the switch: 'Start Word and create a new doc from the template Set objWord = CreateObject("Word.Basic")...
4
3609
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
4
1666
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 expose the regex function to my old legacy application built with some tools that runs a VM with capability to access windows native DLL via...
9
7068
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
5605
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 expects type 'void *; but argument 2 has type 'void
9
3263
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 function are of type int. (My question has nothing to do with the definition of the function foo, so don't bother about it.) If I call the function...
3
4153
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 non-English) characters (e.g. µ). The problem is that I can't load files with such names. The xslt is saved and declared as UTF-8. E.g. the xml...
8
2977
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. Now the query and database are already open so I'm not entirely sure of any connection strings I may need. I think part of the problem may be that the...
0
7695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6281
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.