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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:18:46+00:00 2026-05-11T12:18:46+00:00

It’s possible to define a pointer to a member and using this later on:

  • 0

It’s possible to define a pointer to a member and using this later on:

struct foo {   int a;   int b[2]; };

int main() {
foo bar; int foo::* aptr=&foo::a; bar.a=1; std::cout << bar.*aptr << std::endl; }

Now I need to have a pointer to a specific element of an array, so normally I'd write
int foo::* bptr=&(foo::b[0]);
However, the compiler just complains about an 'invalid use of non-static data member 'foo::b'' Is it possible to do this at all (or at least without unions)?

Edit: I need a pointer to a specific element of an array, so int foo::* ptr points to the second element of the array (foo::b[1]).

Yet another edit: I need to access the element in the array by bar.*ptr=2, as the pointer gets used somewhere else, so it can't be called with bar.*ptr[1]=2 or *ptr=2.

  • 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. 2026-05-11T12:18:46+00:00Added an answer on May 11, 2026 at 12:18 pm

    The problem is that, accessing an item in an array is another level of indirection from accessing a plain int. If that array was a pointer instead you wouldn’t expect to be able to access the int through a member pointer.

    struct foo {   int a;   int *b; };  int main() {    foo bar;   int foo::* aptr=&(*foo::b); // You can't do this either!   bar.a=1;   std::cout << bar.*aptr << std::endl; } 

    What you can do is define member functions that return the int you want:

    struct foo {   int a;   int *b;   int c[2];    int &GetA() { return a; } // changed to return references so you can modify the values   int &Getb() { return *b; }   template <int index>   int &GetC() { return c[index]; } }; typedef long &(Test::*IntAccessor)();  void SetValue(foo &f, IntAccessor ptr, int newValue) {       cout << 'Value before: ' << f.*ptr();     f.*ptr() = newValue;     cout << 'Value after: ' << f.*ptr(); }  int main() {   IntAccessor aptr=&foo::GetA;   IntAccessor bptr=&foo::GetB;   IntAccessor cptr=&foo::GetC<1>;    int local;   foo bar;   bar.a=1;   bar.b = &local;   bar.c[1] = 2;    SetValue(bar, aptr, 2);   SetValue(bar, bptr, 3);   SetValue(bar, cptr, 4);   SetValue(bar, &foo::GetC<0>, 5); } 

    Then you at least have a consistent interface to allow you to change different values for foo.

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

Sidebar

Ask A Question

Stats

  • Questions 241k
  • Answers 241k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It sounds like a trouble with the text encoding. You… May 13, 2026 at 7:29 am
  • Editorial Team
    Editorial Team added an answer Your Javascript code is in a seperate .js file so… May 13, 2026 at 7:29 am
  • Editorial Team
    Editorial Team added an answer Ok so perhaps my answer is only relevant to users… May 13, 2026 at 7:29 am

Related Questions

Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want use html5's new tag to play a wav file (currently only supported
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.