成员指向数组元素的指针 [英] Member pointer to array element
问题描述
可以定义一个指向成员的指针,并在稍后使用它:
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]);
然而,编译器只是抱怨一个
无效使用非静态数据成员'foo :: b'
是否可以这样做(或至少没有工会)?
编辑:我需要一个指向数组的特定元素的指针,所以 int foo :: * ptr
指向数组的第二个元素( foo :: b [1]
)。
还有一个编辑:我需要通过 bar访问数组中的元素。* ptr = 2
,因为指针在其他地方被使用,所以它不能用 bar。* ptr [1] = 2
或 * ptr = 2
。
问题是,访问数组中的项是另一个级别的间接访问一个简单的int。如果该数组是一个指针,而不是期望能够通过成员指针访问int。
struct foo
{
int a;
int * b;
};
int main()
{
foo bar;
int foo :: * aptr =&(* foo :: b); //你也不能这样做!
bar.a = 1;
std :: cout<< bar。* aptr<< std :: endl;
}
你可以做的是定义成员函数,返回你想要的int: / p>
struct foo
{
int a;
int * b;
int c [2];
int& GetA(){return a; } //改为返回引用,所以你可以修改值
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);
}
然后你至少有一个一致的接口,允许你改变不同的值foo。
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
.
The problem is, 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 consistant interface to allow you to change different values on foo.
这篇关于成员指向数组元素的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!