Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6010689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:09:09+00:00 2026-05-23T02:09:09+00:00

Is it possible to declare a member function of a forward-declared class as friend?

  • 0

Is it possible to declare a member function of a forward-declared class as friend? I am trying to do the following:

class BigComplicatedClass;

class Storage {
   int data_;
public:
   int data() { return data_; }
   // OK, but provides too broad access:
   friend class BigComplicatedClass;
   // ERROR "invalid use of incomplete type":
   friend void BigComplicatedClass::ModifyStorage(); 
};

So the goal is to (i) restrict the friend declaration to a single method, and (ii) not to include the definition of the complicated class to reduce compile time.

One approach might be to add a class acting as an intermediary:

// In Storage.h:
class BigComplicatedClass_Helper;
class Storage {
    // (...)
    friend class BigComplicatedClass_Helper;
};

// In BigComplicatedClass.h:
class BigComplicatedClass_Helper {
     static int &AccessData(Storage &storage) { return storage.data_; }
     friend void BigComplicatedClass::ModifyStorage();
};

However, this seems a bit clumsy… so I assume that there must be a better solution!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:09:11+00:00Added an answer on May 23, 2026 at 2:09 am

    As @Ben says, it’s not possible, but you can give specific access just to that member function through a “passkey”. It works a bit like the intermediate helper class, but is imho clearer:

    // Storage.h
    // forward declare the passkey
    class StorageDataKey;
    
    class Storage {
       int data_;
    public:
       int data() { return data_; }
       // only functions that can pass the key to this function have access
       // and get the data as a reference
       int& data(StorageDataKey const&){ return data_; }
    };
    
    // BigComplicatedClass.cpp
    #include "BigComplicatedClass.h"
    #include "Storage.h"
    
    // define the passkey
    class StorageDataKey{
      StorageDataKey(){} // default ctor private
      StorageDataKey(const StorageDataKey&){} // copy ctor private
    
      // grant access to one method
      friend void BigComplicatedClass::ModifyStorage();
    };
    
    void BigComplicatedClass::ModifyStorage(){
      int& data = storage_.data(StorageDataKey());
      // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to forward-declare a nested class, then use it as the type
I have next class and try to declare member function which will return pointer
I get the following x.cpp: In member function ‘X’: x.cpp:153:10: warning: possible problem detected
why or for what reason is it not possible to declare a class member
It's possible to declare variables with the below structure in C++ private: public: protected:
Is it possible to forward declare an standard container in a header file? For
Is it possible to declare a function pointer (non C++ 11) that can point
I have a class, in which i declare a static function. I register this
Inside of a static member function I need to get the type. class MyClass
I got trouble in creating special instance of member template function of non-template class.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.