forward declare member function so that it can be friend function

Discussion in 'C++' started by yancheng.cheok@gmail.com, Nov 9, 2006.

  1. Guest

    currently, i have a private function in cat named privateFun.

    i would like to have this function "private" to all except dog's action
    member function.

    by using the following approach, all the dog's members can access all
    the cat's private members.

    // cat.h
    //
    #ifndef CAT_H
    #define CAT_H

    // forward declaration.
    class dog;

    class cat
    {
    private:
    friend class dog;
    void privateFun() {}
    };

    #endif
    //
    // cat.h

    // dog.h
    //
    #ifndef DOG_H
    #define DOG_H

    #include "cat.h"

    class dog
    {
    private:
    void action()
    {
    cat c;
    c.privateFun();
    }
    };

    #endif
    //
    // dog.h

    however, i would like to have ONLY dog's action member function to
    access cat's private members.

    i try the following approach but can't work.


    // cat.h
    //
    #ifndef CAT_H
    #define CAT_H

    // forward declaration.
    class dog;

    class cat
    {
    private:
    friend void dog::action(); /* HERE IS THE CHANGES AND COMPILATION
    ERROR HAPPENS HERE. */
    void privateFun() {}
    };

    #endif
    //
    // cat.h

    // dog.h
    //
    #ifndef DOG_H
    #define DOG_H

    #include "cat.h"

    class dog
    {
    private:
    void action()
    {
    cat c;
    c.privateFun();
    }
    };

    #endif
    //
    // dog.h

    Of course, i would get the following compilation error:

    c:\Documents and Settings\YC Cheok\Desktop\aaa\cat.h(10): error C2027:
    use of undefined type 'dog'

    However, I just cann't include the dog header file into cat. This will
    introduce circular include problem.

    Any advice? Can I have something like member function forward
    declaration? Thank you very much
    , Nov 9, 2006
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > currently, i have a private function in cat named privateFun.
    >
    > i would like to have this function "private" to all except dog's action
    > member function.
    >
    > by using the following approach, all the dog's members can access all
    > the cat's private members.
    >
    > // cat.h
    > //
    > #ifndef CAT_H
    > #define CAT_H
    >
    > // forward declaration.
    > class dog;
    >
    > class cat
    > {
    > private:
    > friend class dog;
    > void privateFun() {}
    > };
    >
    > #endif
    > //
    > // cat.h
    >
    > // dog.h
    > //
    > #ifndef DOG_H
    > #define DOG_H
    >
    > #include "cat.h"
    >
    > class dog
    > {
    > private:
    > void action()
    > {
    > cat c;
    > c.privateFun();
    > }
    > };
    >
    > #endif
    > //
    > // dog.h
    >
    > however, i would like to have ONLY dog's action member function to
    > access cat's private members.
    >
    > i try the following approach but can't work.
    >
    >
    > // cat.h
    > //
    > #ifndef CAT_H
    > #define CAT_H
    >
    > // forward declaration.
    > class dog;
    >
    > class cat
    > {
    > private:
    > friend void dog::action(); /* HERE IS THE CHANGES AND COMPILATION
    > ERROR HAPPENS HERE. */
    > void privateFun() {}
    > };
    >
    > #endif
    > //
    > // cat.h
    >
    > // dog.h
    > //
    > #ifndef DOG_H
    > #define DOG_H
    >
    > #include "cat.h"
    >
    > class dog
    > {
    > private:
    > void action()
    > {
    > cat c;
    > c.privateFun();
    > }
    > };
    >
    > #endif
    > //
    > // dog.h
    >
    > Of course, i would get the following compilation error:
    >
    > c:\Documents and Settings\YC Cheok\Desktop\aaa\cat.h(10): error C2027:
    > use of undefined type 'dog'
    >
    > However, I just cann't include the dog header file into cat. This will
    > introduce circular include problem.
    >
    > Any advice? Can I have something like member function forward
    > declaration? Thank you very much


    Make sure you break up the implementation from the declaration. So you
    should be able to include dog.h from cat.h as long as dog.h doesn't actually
    have any code. This is untested code, but should show you how to fix the
    problem:

    // dog.h
    class cat; // forward declaration
    class dog
    {
    private:
    void action();
    cat c;
    };

    // cat.h
    #include "dog.h"
    class cat
    {
    private:
    friend void dog::action(); // This should work now
    void privateFun() {}
    };

    // dog.cpp
    #include "dog.h"
    #include "cat.h"
    void dog::action()
    {
    c.privateFun();
    }

    // cat.cpp
    #include "cat.h"
    void cat::privateFun()
    {
    }
    Jim Langston, Nov 9, 2006
    #2
    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. freegnu
    Replies:
    2
    Views:
    330
    freegnu
    Oct 23, 2006
  2. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    693
    Victor Bazarov
    Aug 1, 2007
  3. Replies:
    4
    Views:
    431
    Victor Bazarov
    Sep 30, 2007
  4. Replies:
    2
    Views:
    664
    Triple-DES
    Feb 26, 2008
  5. Rui Maciel
    Replies:
    11
    Views:
    1,084
    Francesco S. Carta
    Jul 13, 2010
Loading...

Share This Page