Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. forward declare member function so that it can be friend function

forward declare member function so that it can be friend function

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yccheok
    wrote on last edited by
    #1

    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

    X 1 Reply Last reply
    0
    • Y yccheok

      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

      X Offline
      X Offline
      XtremDev
      wrote on last edited by
      #2

      You can declare the other cat function as protected. In that case, the dog can not access to that function

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups