Results 1 to 11 of 11

Thread: static / friend functions

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  3. #3
    Lively Member
    Join Date
    Nov 2005
    Posts
    68

    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...)

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    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

  6. #6
    Lively Member
    Join Date
    Nov 2005
    Posts
    68

    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...
    "bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    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.

  8. #8
    Lively Member
    Join Date
    Nov 2005
    Posts
    68

    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...)

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    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

  11. #11
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    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
  •  



Click Here to Expand Forum to Full Width