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

  • Home
  • SEARCH
  • 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 7662669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:51:35+00:00 2026-05-31T13:51:35+00:00

I want a type A that will yield its hidden datum to an object

  • 0

I want a type A that will yield its hidden datum to an object of type T but hide the datum from everyone else. My C++ compiler happens to be GCC 4.4, but that shouldn’t matter. Why won’t this work?

#include <iostream>

template <class T> class A {
  private:
    int n1;
  public:
    friend class T;
    A(const int n0 = 0) : n1(n0) {}
};

class B {
  public:
    int f(const A<B> a) const { return a.n1; }
    B() {}
};

int main() {
    const A<B> a(5);
    const B b;
    const int m = b.f(a);
    std::cout << m << "\n";
    return 0;
}

Incidentally, this works fine, except that it fails to hide the datum:

#include <iostream>

template <class T> class A {
  private:
    int n1;
  public:
    int n() const { return n1; }
    A(const int n0 = 0) : n1(n0) {}
};

class B {
  public:
    int f(const A<B> a) const { return a.n(); }
    B() {}
};

int main() {
    const A<B> a(5);
    const B b;
    const int m = b.f(a);
    std::cout << m << "\n";
    return 0;
}

Does C++ really not allow a friend class to be specified at compile time as a template parameter? Why not? If not, then what alternate technique should I use to hide the datum? (One would prefer a compile-time technique if possible.)

What is my misunderstanding here, please?

(I see some answers to related questions here and here, but either they don’t answer my particular question or I fail to understand that they do so. At any rate, maybe I am using the wrong technique altogether. Though I remain interested in why the friend class T fails, what I really want to know is how to hide the datum, whether with a friend or by other means.)

Thanks.

  • 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-31T13:51:36+00:00Added an answer on May 31, 2026 at 1:51 pm

    I don’t know the standardese behind your error (refer to Xeo’s answer), but I did find a workaround for C++03.
    Instead of making T a friend, make one of T‘s member functions a friend:

    #include <iostream>
    
    template <class T> class A {
      private:
        int n1;
      public:
        friend int T::getN1(const A& a) const;
        A(const int n0 = 0) : n1(n0) {}
    };
    
    class B {
      public:
        int f(const A<B> a) const { return getN1(a); }
        B() {}
      private:
        int getN1(const A<B>& a) const {return a.n1;}
    };
    
    class C {
      public:
        int f(const A<B> a) const { return getN1(a); }
        C() {}
      private:
        // Error, n1 is a private member of A<B>
        int getN1(const A<B>& a) const {return a.n1;}
    };
    
    int main() {
        const A<B> a(5);
        const B b;
        const int m = b.f(a);
        std::cout << m << "\n";
        return 0;
    }
    

    Alternatively, you can make a nested class/struct of T be a friend of A. This may be more convenient if there are several private members of A that you want T to have access to.

    #include <iostream>
    
    template <class T> class A {
      private:
        int n1;
      public:
        friend class T::AccessToA;
        A(const int n0 = 0) : n1(n0) {}
    };
    
    class B {
      public:
        int f(const A<B> a) const { return AccessToA::getN1(a); }
        B() {};
      private:
        friend class A<B>;
        struct AccessToA
        {
            static int getN1(const A<B>& a) {return a.n1;}
        };
    };
    
    class C {
      public:
        int f(const A<B> a) const { return AccessToA::getN1(a); }
        C() {};
    
      private:
        friend class A<C>;
        struct AccessToA
        {
            // Error, n1 is a private member of A<B>
            static int getN1(const A<B>& a) {return a.n1;}
        };
    };
    
    int main() {
        const A<B> a(5);
        const B b;
        const int m = b.f(a);
        std::cout << m << "\n";
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define that the first element will be of a specific type,
I have an object of type Hash that I want to loop over via
I want to get a type of a BasePage object that I am creating.
I want to create a custom field type (MyCompLookup) that will be used in
I want to add metadata to my object graph for non-domain type data that
I want to build a custom content type that will be the basis of
I want to convert the items to a String array or the type that
I want to have a text box that the user can type in that
I want to add two lists of a numeric type such that addedList[x] =
If I want to declare a newtype such that type type of the value

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.