成员指向数组元素的指针

tst*_*ner 14 c++ pointers class

可以定义指向成员的指针,并在以后使用它:

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; }

现在我需要一个指向数组特定元素的指针,所以通常我会写.
int foo::* bptr=&(foo::b[0]);
但是,编译器只是抱怨"invalid use of non-static data member 'foo::b'"是否可以这样做(或者至少没有工会)?

编辑:我需要一个指向数组特定元素的指针,因此int foo::* ptr指向数组的第二个元素(foo::b[1]).

还有另一个编辑:我需要访问数组中的元素bar.*ptr=2,因为指针在其他地方被使用,因此无法使用bar.*ptr[1]=2或调用它*ptr=2.

dir*_*tly 5

但是,编译器只是抱怨"无效使用非静态数据成员'foo :: b'"

这是因为foo::a并且foo::b有不同的类型.更具体地说,foo::bints 的大小为2的数组.您的指针声明必须兼容,即:

int (foo::*aptr)[2]=&foo::b;
Run Code Online (Sandbox Code Playgroud)

是否可以这样做(或者至少没有工会)?

是的,见下文:

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

int main()
{

  foo bar;

  int (foo::*aptr)[2]=&foo::b;
  /* this is a plain int pointer */
  int *bptr=&((bar.*aptr)[1]);

  bar.a=1; 
  bar.b[0] = 2;
  bar.b[1] = 11;

  std::cout << (bar.*aptr)[1] << std::endl;
  std::cout << *bptr << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

根据OP的要求更新了帖子.


Ecl*_*pse 5

问题在于,访问数组中的项目是访问普通 int 的另一级间接。如果该数组是一个指针,那么您就不会期望能够通过成员指针访问 int 。

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;
}
Run Code Online (Sandbox Code Playgroud)

您可以做的是定义返回您想要的 int 的成员函数:

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);
}
Run Code Online (Sandbox Code Playgroud)

那么你至少有一个一致的界面来允许你更改 foo 的不同值。

  • 是的,我只是认为这应该是可能的,因为实际的数组元素可能/应该距对象的开头有一个固定的偏移量。感谢您的澄清。 (2认同)