By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

static functions in a class

P: 1
Consdier the scenario

class A
{
private:
int a;
public:

static void AStatic( A * ptr)

{
ptr->a= 19;

}


} ;

main()
{
A aobj;
A::AStatic(&aobj);




}



This piece of code works
I donot understand how can a private member be accessed directly in the static function
As i think static functions are only for static memebrs adn accessing public data of the object passed is ok
but how cum private data is accesed.
Aug 4 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
I donot understand how can a private member be accessed directly in the static function
As i think static functions are only for static memebrs adn accessing public data of the object passed is ok
but how cum private data is accesed.
The access rules apply to static members/functions as well, i.e. that static
function has all the right to fiddle with private members, either static or not.
Those access rules are defined at class level, not instantiation level. If both
X and Y are of that same class, X and Y can fiddle with each others' private
members/functions.

kind regards,

Jos
Aug 4 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.