Can static member funcs be friends?

Discussion in 'C++' started by Improving, Oct 6, 2005.

  1. Improving

    Improving Guest

    why doesnt A::func have access to a B objects internals?

    #include<iostream>
    using namespace std;
    class A;
    class B
    {
    friend class A;
    int a;

    public:
    B(int b) : a(b)
    {;}
    };

    class A
    {
    static void func(B* b)
    {
    cout<< b->a << endl;
    }
    };

    int main()
    {
    B b(10);
    A::func(&b);
    cin.get();
    return 0;
    }
     
    Improving, Oct 6, 2005
    #1
    1. Advertising

  2. Improving

    sat Guest

    Yes , there is nothing wrong with your code. It should run fine, but for one
    hiccup.

    int main()
    > {
    > B b(10);
    > A::func(&b); // this is an error
    >


    func in class A is by default a private member.. (though it is static),
    hence your program might have not compiled.
    try putting it in public scope. I thnk the program should go thro' fine.



    "Improving" <> wrote in message
    news:...
    > why doesnt A::func have access to a B objects internals?
    >
    > #include<iostream>
    > using namespace std;
    > class A;
    > class B
    > {
    > friend class A;
    > int a;
    >
    > public:
    > B(int b) : a(b)
    > {;}
    > };
    >
    > class A
    > {
    > static void func(B* b)
    > {
    > cout<< b->a << endl;
    > }
    > };
    >
    > int main()
    > {
    > B b(10);
    > A::func(&b);
    > cin.get();
    > return 0;
    > }
    >
     
    sat, Oct 6, 2005
    #2
    1. Advertising

  3. On 6 Oct 2005 03:19:14 -0700, "Improving" <>
    wrote:

    >why doesnt A::func have access to a B objects internals?


    It does ... but you need to make A::func() public in order to call it
    from main().

    >#include<iostream>
    >using namespace std;
    >class A;
    >class B
    >{
    > friend class A;
    > int a;
    >
    >public:
    > B(int b) : a(b)
    > {;}
    >};
    >
    >class A
    >{
    > static void func(B* b)
    > {
    > cout<< b->a << endl;
    > }
    >};
    >
    >int main()
    >{
    > B b(10);
    > A::func(&b);
    > cin.get();
    > return 0;
    >}


    --
    Bob Hairgrove
     
    Bob Hairgrove, Oct 6, 2005
    #3
  4. Improving

    Rolf Magnus Guest

    Improving wrote:

    > #include<iostream>
    > using namespace std;
    > class A;
    > class B
    > {
    > friend class A;
    > int a;
    >
    > public:
    > B(int b) : a(b)
    > {;}
    > };
    >
    > class A
    > {


    public:

    > static void func(B* b)
    > {
    > cout<< b->a << endl;
    > }
    > };
    >
    > int main()
    > {
    > B b(10);
    > A::func(&b);
    > cin.get();
    > return 0;
    > }
     
    Rolf Magnus, Oct 6, 2005
    #4
  5. Improving

    Improving Guest

    OMFG what a plonker I am!
     
    Improving, Oct 6, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    6
    Views:
    404
    Rob Williscroft
    Nov 3, 2003
  2. Thomas Matthews

    Ptrs to member funcs & inheritance

    Thomas Matthews, Dec 20, 2003, in forum: C++
    Replies:
    2
    Views:
    388
    David White
    Dec 20, 2003
  3. Replies:
    2
    Views:
    616
  4. Replies:
    0
    Views:
    681
  5. dolphin
    Replies:
    3
    Views:
    1,399
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page