static / friend functions
Is it possible to either...
a) Allow a static member function to access private / protected members of the class
b) Brings a friend function into the same namespace as the class it befriends
Why? Because I want to do something like this:
Code:
CTextFile* text = CTextFile::LoadFile ("C:\\Test.txt");
It's how I usually form loading functions in C# and I'm hoping to do something similar in C++. Is this possible? It just seems silly that a static member function can't access the private members of a class, even though it is a member. :ehh:
Re: static / friend functions
Static members already have access to private/protected members. All you need is an instance pointer.
Re: static / friend functions
Static members can NOT access non static members
Re: static / friend functions
Quote:
#include <iostream>
class Test
{
int member;
public:
Test() : member(100) {}
static void test()
{
Test instance;
std::cout << instance.member << std::endl;
}
};
int main()
{
Test::test();
}
As I said, they can. All you need is an instance. But this is not about instances (I think), just about access. Static methods can access privates and protecteds of their containing classes.
Re: static / friend functions
Strange, I could've sworn that this didn't work earlier (though I'm pretty sure I had got it working once before as well).
Thanks for your help. :)
Re: static / friend functions
Quote:
Originally Posted by CornedBee
As I said, they can. All you need is an instance. But this is not about instances (I think), just about access. Static methods can access privates and protecteds of their containing classes.
From my point of view accessing a class member means
member = someValue; memberFunction(); for private or public members
With your definition of access any function can access public (and only public) members of an instance...
Re: static / friend functions
Quote:
Originally Posted by bilm_ks
From my point of view accessing a class member means
member = someValue; memberFunction(); for private or public members
With your definition of access any function can access public (and only public) members of an instance...
Your point of view, however, is ignorant of the premise I put in my post. Which makes the emphasis you put on "not" (which suggested to me that you were explicitely disagreeing with me) rather silly.
Re: static / friend functions
Static member functions are considered to have class scope. In contrast to nonstatic member functions, these functions have no implicit this argument; therefore, they can use only static data members, enumerators, or nested types directly. (from C++ language reference in static member functions)
Re: static / friend functions
Yes, we know that. You still fail to see the point of the original question, which was clearly about access control, not instances.
Re: static / friend functions
Just to clarify what I did mean, here is an example of how the function I was trying (and have since succeeded) to create:
Code:
// 'Load' is a static function
cTextFile* cTextFile::Load (const char* name)
{
cTextFile* obj = new cTextFile ();
obj->ReadFileData (name); // ReadFileData is a private function
return obj;
}
Hopefully that clears things up. :)
Re: static / friend functions
i would highly recommend not trying to get your c++ code to act like c#