Giving static member functions access to protected functions 
Author Message
 Giving static member functions access to protected functions

Is there a way I can use "friend" to give access to a static member function
so that it can access private member functions that are in it's own class?


Thu, 12 Feb 2004 11:49:36 GMT  
 Giving static member functions access to protected functions
Hi, Bruce!

Quote:

> Is there a way I can use "friend" to give access to a static member function
> so that it can access private member functions that are in it's own class?

I think a static member function already has that access privilege -- what problem(s) are you having
exactly?

Jeff...
--
Please post all follow-ups to the newsgroup only.



Thu, 12 Feb 2004 18:11:15 GMT  
 Giving static member functions access to protected functions
A static member function canNOT use non-static member variables. A static
member function has NO implicit 'this' pointer so do not expect to be able
to reference variables that imply a 'this' pointer by any method.

eg.

class CMyClass
{
    static void MyFunc1(); // can use m_sint but not m_int
    void MyFunc2(); // can use m_sint and m_int
    static int m_sint;  // does not imply a this pointer and can be used in
static member function
    int m_int; // does imply a this pointer and cannot be used in static
member function

Quote:
}



Quote:
> Is there a way I can use "friend" to give access to a static member
function
> so that it can access private member functions that are in it's own class?



Thu, 12 Feb 2004 19:19:42 GMT  
 Giving static member functions access to protected functions

Quote:
>Is there a way I can use "friend" to give access to a static member function
>so that it can access private member functions that are in it's own class?

Bruce,

static member functions can only access static member variables.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Thu, 12 Feb 2004 19:14:32 GMT  
 Giving static member functions access to protected functions
Sorry I should clarify. I want a static function to be able to execute
another static function.  Plus one class is derived from the other.  Perhaps
an example would explain it better.

This gives the compile error:
 error C2248: 'foo1' : cannot access private member declared in class
'MyClass

I tried making MyClass1 a friend of MyClass and visa versa but that didn't
work.

class MyClass
{
private:
 static void foo1();
 static void foo2();

Quote:
};

class MyClass2 : public MyClass
{

Quote:
};

void MyClass::foo1()
{

Quote:
}

void MyClass::foo2()
{
  MyClass2 *m = new MyClass2;
  m->foo1();

Quote:
}



Quote:
> Is there a way I can use "friend" to give access to a static member
function
> so that it can access private member functions that are in it's own class?



Fri, 13 Feb 2004 10:46:19 GMT  
 Giving static member functions access to protected functions

Quote:
>Sorry I should clarify. I want a static function to be able to execute
>another static function.  Plus one class is derived from the other.  Perhaps
>an example would explain it better.

Bruce,

If you make your MyClass::foo1 function public, then you can call it
by doing:

void MyClass::foo2()
{
        MyClass2::foo1();

Quote:
}

A static function is really nothing more useful than a restricted
global function.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Fri, 13 Feb 2004 16:35:33 GMT  
 Giving static member functions access to protected functions
Thanks,

So I guess I am just stuck with making it public


Quote:
> >Sorry I should clarify. I want a static function to be able to execute
> >another static function.  Plus one class is derived from the other.
Perhaps
> >an example would explain it better.

> Bruce,

> If you make your MyClass::foo1 function public, then you can call it
> by doing:

> void MyClass::foo2()
> {
> MyClass2::foo1();
> }

> A static function is really nothing more useful than a restricted
> global function.

> Dave
> --
> MVP VC++ FAQ: http://www.mvps.org/vcfaq
> My address is altered to discourage junk mail.
> Please post responses to the newsgroup thread,
> there's no need for follow-up email copies.



Sat, 14 Feb 2004 06:45:29 GMT  
 Giving static member functions access to protected functions
Hi, Bruce!

Quote:

> Thanks,

> So I guess I am just stuck with making it public

In the example you posted, it is the derived class that hasn't the access privilege -- and the base
class can't get to it's own private function through the derived class' access restrictions. You
could use a cast...

    ((CBase*)pDerived)->PrivateFunc();

...but ultimately why bother. You don't need a class instance at all, just...

   CBase::PrivateFunc();

HTH,

Jeff...
--
Please post all follow-ups to the newsgroup only.



Sat, 14 Feb 2004 06:55:34 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. static function access member variable and member function

2. strange problem occur while trying to access member function through static function

3. Accessing non-static functions from a static function

4. Getting pointer to non-static member function from C callback function

5. static function calles member function?

6. Using Non-Static Callback Functions as member Functions VC5.0

7. Private member access in a different function member

8. One Problem in Calling Static function from Non static function

9. how to call a non-static member function of a class from a static member function of the class?

10. ?? COMPILER BUG in MSVC++: operator ? allows static member functions to access non-static members ??

11. C++, forming pointer to static member function

12. static member functions

 

 
Powered by phpBB® Forum Software