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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:27:32+00:00 2026-06-01T02:27:32+00:00

In reference to May pointer to members circumvent the access level of a member?

  • 0

In reference to May pointer to members circumvent the access level of a member?, I would like to understand the code snippet in that question.

I will paste the code snippet here.

#include <iostream>

template<typename Tag, typename Tag::type M>
struct Rob { 
  friend typename Tag::type get(Tag) {
    return M;
  }
};

// use
struct A {
  A(int a):a(a) { }
private:
  int a;
};

// tag used to access A::a
struct A_f { 
  typedef int A::*type;
  friend type get(A_f);
};

template struct Rob<A_f, &A::a>;

int main() {
  A a(42);
  std::cout << "proof: " << a.*get(A_f()) << std::endl;
}

Among a few questions, I highlight a single question here that may crack open the rest.

I do not understand the following statement in the main function:

a.*get(A_f())

I do understand (I think) that get(A_F()) returns a pointer to a.a. However, I do not understand the get() function. In what struct is this actually defined? How is it being accessed in that struct?

I have two secondary questions that may be answered if the above question is answered, but I will place them here. First, in the definition of Rob, the friend keyword is being used in front of the function get that is being defined, as well as declared. I thought that the friend keyword can only be used in front of a function declaration to indicate that a function defined elsewhere has access to the private/protected members of the class. Can someone explain?

Second, I do not understand the line template struct Rob<A_f, &A::a>;. I do not understand the use of the template keyword without <...>, and I do not understand why there is no template definition – this seems to be some sort of forward declaration. Can someone explain? 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-06-01T02:27:33+00:00Added an answer on June 1, 2026 at 2:27 am

    However, I do not understand the get() function. In what struct is this actually defined? How is it being accessed in that struct?

    First thing first: the functionget() is a friend of both A and Rob class template. It is not a member function of any class. It is rather a free function.

    Now, where exactly is this free function defined? Well it is defined in Rob class template, and at the same time it is declared as friend of Rob class template.

    I thought that the friend keyword can only be used in front of a function declaration to indicate that a function defined elsewhere has access to the private/protected members of the class. Can someone explain?

    Yes it can be declared friend of a class, without defining it inside the class. It can also be defined inside a class and at the same time can be declared friend of that class. In your case, it is actually defined in the class itself.

    Second, I do not understand the line template struct Rob<A_f, &A::a>;.

    That is called explicit instantiation of the class template with the given tempate arguments. You can see many other syntax to explicitly instantiate a template here (well if it is a function template):

    • C++ Template Specialization and Instantiation Syntax
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this may be a stupid question but when I add a reference to my
I have a reference to call that may or may not be there. When
Say I have a function that takes a const reference to a pointer... Example:
I'm still learning c++ and have a question that may be obvious, or maybe
The formulation of the question may be confusing. I do understand why it is
I can't think of an example where the reference count of a file may
I've written a DLL that may be used in a number of ways (referenced
For reference, the code is for the motorola 68008. Say I have code such
In reference to this answer to a Stack Overflow question : what is bench-testing
I have always been taught that non-primitive types should be passed by const reference

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.