C++ 指针数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2879700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Array Of Pointers
提问by Thomas
In C++ if I want an array of pointers so that I may have them point to different objects at a latter stage what does the syntax look like.
在 C++ 中,如果我想要一个指针数组,以便我可以在后期让它们指向不同的对象,语法是什么样的。
EDIT
编辑
I need to clarify what I am trying to do I guess. I have a class foo that has and add method. In the add method I take a reference to class bar. I want to save that reference into a pointer array of bar. The array of pointer bar will need to be expanded all the time this I have no problems with. It is creating and array on the heap of pointers so that I may assign bar objects to them later. What I have tried seems to fail as class bar does not have a default constructor which the compiler is complaining about. This lead me to think that I was creating and array of actual objects which I did not want to do.
我需要澄清我想做什么。我有一个 foo 类,它有和 add 方法。在 add 方法中,我引用了类 bar。我想将该引用保存到 bar 的指针数组中。指针条的数组将需要一直扩展,这我没有问题。它在指针堆上创建和数组,以便我稍后可以将 bar 对象分配给它们。我尝试过的似乎失败了,因为 class bar 没有编译器抱怨的默认构造函数。这让我认为我正在创建和我不想做的实际对象数组。
Please no stl and no I do not want to hear about how you think this is crazy etc that is your opinion.
请不要stl,也不要我不想听到你认为这很疯狂等等你的意见。
回答by Joey Adams
What you want is:
你想要的是:
Foo *array[10]; // array of 10 Foo pointers
Not to be confused with:
不要与以下内容混淆:
Foo (*array)[10]; // pointer to array of 10 Foos
In either case, nothing will be automatically initialized because these represent pointers to Foos that have yet to be assigned to something (e.g. with new).
在任何一种情况下,都不会自动初始化任何东西,因为它们表示尚未分配给某些东西(例如,使用 new)的 Foos 指针。
I finally "got" pointer/array declaration syntax in C when I realized that it describes how you access the base type. Foo *array[5][10];
means that *array[0..4][0..9]
(subscript on an array of 5 items, then subscript on an array of 10 items, then dereference as a pointer) will access a Foo object (note that []
has higher precedence than *
).
当我意识到它描述了如何访问基类型时,我终于在 C 中“获得”了指针/数组声明语法。 Foo *array[5][10];
意味着*array[0..4][0..9]
(5 个项目的数组上的下标,然后 10 个项目的数组上的下标,然后作为指针取消引用)将访问 Foo 对象(请注意,[]
其优先级高于*
)。
This seems backwards. You would think that int array[5][10];
(a.k.a. int (array[5])[10];
) is an array of 10 int array[5]
. Suppose this were the case. Then you would access the last element of the array by saying array[9][4]
. Doesn't that look backwards too? Because a C array declaration is a pattern indicating how to get to the base type (rather than a composition of array expressions like one might expect), array declarations and code using arrays don't have to be flipflopped.
这似乎倒退了。你会认为int array[5][10];
(aka int (array[5])[10];
) 是一个 10 的数组int array[5]
。假设是这种情况。然后你可以通过说访问数组的最后一个元素array[9][4]
。这不是也向后看吗?因为 C 数组声明是一种指示如何获得基本类型的模式(而不是像人们期望的那样由数组表达式组合而成),所以不必翻转数组声明和使用数组的代码。
回答by Naveen
For example, if you want an array of int
pointers it will be int* a[10]
. It means that variable a
is a collection of 10 int*
s.
例如,如果您想要一个int
指针数组,它将是int* a[10]
. 这意味着变量a
是 10int*
秒的集合。
EDIT
编辑
I guess this is what you want to do:
我想这就是你想要做的:
class Bar
{
};
class Foo
{
public:
//Takes number of bar elements in the pointer array
Foo(int size_in);
~Foo();
void add(Bar& bar);
private:
//Pointer to bar array
Bar** m_pBarArr;
//Current fee bar index
int m_index;
};
Foo::Foo(int size_in) : m_index(0)
{
//Allocate memory for the array of bar pointers
m_pBarArr = new Bar*[size_in];
}
Foo::~Foo()
{
//Notice delete[] and not delete
delete[] m_pBarArr;
m_pBarArr = NULL;
}
void Foo::add(Bar &bar)
{
//Store the pointer into the array.
//This is dangerous, you are assuming that bar object
//is valid even when you try to use it
m_pBarArr[m_index++] = &bar;
}
回答by triktae
I would do it something along these lines:
我会按照以下方式做一些事情:
class Foo{
...
};
int main(){
Foo* arrayOfFoo[100]; //[1]
arrayOfFoo[0] = new Foo; //[2]
}
[1] This makes an array of 100 pointers to Foo-objects. But no Foo-objects are actually created.
[1] 这构成了一个包含 100 个指向 Foo 对象的指针的数组。但实际上并没有创建 Foo 对象。
[2] This is one possible way to instantiate an object, and at the same time save a pointer to this object in the first position of your array.
[2] 这是实例化对象的一种可能方法,同时将指向该对象的指针保存在数组的第一个位置。
回答by Potatoswatter
If you don't use the STL, then the code looks a lotbit like C.
如果你不使用STL,那么代码看起来很多有点像C.
#include <cstdlib>
#include <new>
template< class T >
void append_to_array( T *&arr, size_t &n, T const &obj ) {
T *tmp = static_cast<T*>( std::realloc( arr, sizeof(T) * (n+1) ) );
if ( tmp == NULL ) throw std::bad_alloc( __FUNCTION__ );
// assign things now that there is no exception
arr = tmp;
new( &arr[ n ] ) T( obj ); // placement new
++ n;
}
T
can be any POD type, including pointers.
T
可以是任何 POD 类型,包括指针。
Note that arr
must be allocated by malloc
, not new[]
.
请注意,arr
必须由 分配malloc
,而不是new[]
。