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 3855150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:43:54+00:00 2026-05-19T17:43:54+00:00

assume that I have the following two template classes : template <class _A> class

  • 0

assume that I have the following two template classes :

template <class _A>
class First
{
private:
    int a;
};

template <class _B>
class Second
{
private:
    int b;
};

how can I link them in many-to-many friendship. for example, adding a method in First that prints b of a parameter object of Second.

is my question clear?

  • 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-19T17:43:55+00:00Added an answer on May 19, 2026 at 5:43 pm
    template <typename T>
    class First {
       int a;
       template<typename> friend class Second;
    };
    template <typename T>
    class Second
    {
       int b;
       template<typename> friend class First;
    };
    

    This will enable every First<T> to access the internals of every Second<U>. Now, while this is the technical solution, you might want to consider whether a design with cyclic dependencies and opening up the internal to any instantiation of the other class is the best solution to your particular problem.

    BTW, if you only want to grant First<int> access to Second<int> (and not Second<double>) you can do that like so:

    template <typename> class Second;
    template <typename T>
    class First {
       int a;
       friend class Second<T>; // only befriend the same instantiation
    };
    template <typename T>
    class Second {
       int b;
       friend class First<T>;
    };
    

    In this second version you need the forward declaration of the Second template before befriending a particular instantiation, but this allows you to grant access to the internals of the class only to a particular instantiation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let us assume we have the two following classes: abstract case class MyParent(param: Int)
Assume I have the following two classes: public class User : Entity { public
Assume that we have the following code: class Program { static volatile bool flag1;
Let's assume that we have following jQuery plugins (each of them in separate files):
Assume that i have two strings like the following. $sa = 12,20,45; $sb =
Let's assume that I have two tables... Foo and Bar. They contain the following
Assume that I have two entities as follows: class Blog { public Blog() {
Assume that we have multiple arrays of integers. You can consider each array as
To illustrate, assume that I have two tables as follows: VehicleID Name 1 Chuck
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items

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.