|
-
Jan 11th, 2006, 10:22 AM
#1
Thread Starter
Addicted Member
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.
Last edited by Barguast; Jan 11th, 2006 at 10:30 AM.
Using Visual Studio .NET 2005
-
Jan 11th, 2006, 11:42 AM
#2
Re: static / friend functions
Static members already have access to private/protected members. All you need is an instance pointer.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 11th, 2006, 05:14 PM
#3
Lively Member
Re: static / friend functions
Static members can NOT access non static members
"bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)
-
Jan 11th, 2006, 05:20 PM
#4
Re: static / friend functions
#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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 12th, 2006, 04:38 AM
#5
Thread Starter
Addicted Member
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.
Using Visual Studio .NET 2005
-
Jan 12th, 2006, 08:07 AM
#6
Lively Member
Re: static / friend functions
 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...
"bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)
-
Jan 12th, 2006, 09:47 AM
#7
Re: static / friend functions
 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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 13th, 2006, 09:23 AM
#8
Lively Member
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)
"bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)
-
Jan 13th, 2006, 08:06 PM
#9
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 14th, 2006, 03:43 PM
#10
Thread Starter
Addicted Member
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.
Using Visual Studio .NET 2005
-
Jan 15th, 2006, 11:32 PM
#11
Frenzied Member
Re: static / friend functions
i would highly recommend not trying to get your c++ code to act like c#
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|