I'm a Java programer lost in C++ and pointers :D
我是一个在C ++和指针中丢失的Java程序员:D
I have an array of pointers to Bucket-Objects
我有一个指向Bucket-Objects的指针数组
Bucket<E>* index = new Bucket<E>[2];
I initialize it like this:
我像这样初始化它:
index[0] points to Bucket1
index[1] points to Bucket2
And then I want to double the size of the array and link the additional entries like this:
然后我想将数组的大小加倍并链接其他条目,如下所示:
index[0] points to Bucket1
index[1] points to Bucket2
index[2] points to Bucket1
index[3] points to Bucket2
I have this code so far, which generates copies of the Bucket-Objects and I don't want that!
到目前为止我有这个代码,它生成Bucket-Objects的副本,我不希望这样!
for (size_t i = 0; i < newSize; ++i)
{
if (i < oldIndexSize)
newIndex[i] = index[i];
else
newIndex[i] = index[i - oldIndexSize];
}
4 个解决方案
#1
2
I have an array of pointers to Bucket-Objects
我有一个指向Bucket-Objects的指针数组
No, you have a pointer to an array of Bucket objects. An array of pointers to Bucket objects would look like this:
不,你有一个指向Bucket对象数组的指针。指向Bucket对象的指针数组如下所示:
Bucket<E> * index[2];
A dynamically allocated array of pointers to bucket objects would be declared like this:
动态分配的桶对象指针数组将如下所示:
Bucket<E> ** index = new Bucket<E>*[N];
But what you should probably be using is a vector of shared pointers to bucket objects, like this:
但你应该使用的是一个桶对象的共享指针向量,如下所示:
std::vector<std::shared_ptr<Bucket<E>>> index;
#2
1
I'm a Java programer lost in C++ and pointers :D
我是一个在C ++和指针中丢失的Java程序员:D
In modern C++, raw pointers are rarely used. You should prefer smart pointers (like shared_ptr
or unique_ptr
). This gives several benefits, like helping make your code exception-safe, simplifying your code (e.g. resources are automatically destructed and released, there is no need to call an explicit delete
), etc.
在现代C ++中,很少使用原始指针。您应该更喜欢智能指针(如shared_ptr或unique_ptr)。这提供了一些好处,例如帮助使代码异常安全,简化代码(例如,资源被自动销毁和释放,不需要调用显式删除)等。
I have an array of pointers to Bucket-Objects
我有一个指向Bucket-Objects的指针数组
Bucket* index = new Bucket[2];
Bucket * index = new Bucket [2];
If the Bucket
instances you store in the array are not shared, you can use vector<unique_ptr<Bucket>>
. Else, if there is a shared semantics, you may want to use vector<shared_ptr<Bucket>>
. You can use vector::push_back()
method to add new instances of Bucket
's to the vector, which will dynamically grow. If you choose to use shared_ptr<>
smart pointer, allocate the instances of Bucket
's using make_shared
instead of raw operator new
.
如果存储在数组中的Bucket实例未共享,则可以使用vector
There is a very interesting series of STL lessons on Channel 9, by the STL maintainer working in Visual C++ Team. You may want to consider part 1 (sequence containers) and part 3 (smart pointers).
在Visual C ++团队中工作的STL维护者在第9频道上有一系列非常有趣的STL课程。您可能需要考虑第1部分(序列容器)和第3部分(智能指针)。
#3
1
Do not re-invent the wheel. Use std::vector<Bucket<E> >
if you want a resizeable array. If you don't want the buckets to be copied over, you will have to use some indirection. For instance with smart pointers:
不要重新发明*。如果需要可调整大小的数组,请使用std :: vector
std::vector<std::shared_ptr<Bucket<E>>> index {std::make_shared<Bucket<E>>()/*, ...*/}
#4
0
If it is an Array of pointers it will not creat the copy of object it will be only a copy of a pointer and it seems that this code does the thing you want.
如果它是一个指针数组,它将不会创建对象的副本,它将只是一个指针的副本,而且看起来这个代码可以完成你想要的东西。
#1
2
I have an array of pointers to Bucket-Objects
我有一个指向Bucket-Objects的指针数组
No, you have a pointer to an array of Bucket objects. An array of pointers to Bucket objects would look like this:
不,你有一个指向Bucket对象数组的指针。指向Bucket对象的指针数组如下所示:
Bucket<E> * index[2];
A dynamically allocated array of pointers to bucket objects would be declared like this:
动态分配的桶对象指针数组将如下所示:
Bucket<E> ** index = new Bucket<E>*[N];
But what you should probably be using is a vector of shared pointers to bucket objects, like this:
但你应该使用的是一个桶对象的共享指针向量,如下所示:
std::vector<std::shared_ptr<Bucket<E>>> index;
#2
1
I'm a Java programer lost in C++ and pointers :D
我是一个在C ++和指针中丢失的Java程序员:D
In modern C++, raw pointers are rarely used. You should prefer smart pointers (like shared_ptr
or unique_ptr
). This gives several benefits, like helping make your code exception-safe, simplifying your code (e.g. resources are automatically destructed and released, there is no need to call an explicit delete
), etc.
在现代C ++中,很少使用原始指针。您应该更喜欢智能指针(如shared_ptr或unique_ptr)。这提供了一些好处,例如帮助使代码异常安全,简化代码(例如,资源被自动销毁和释放,不需要调用显式删除)等。
I have an array of pointers to Bucket-Objects
我有一个指向Bucket-Objects的指针数组
Bucket* index = new Bucket[2];
Bucket * index = new Bucket [2];
If the Bucket
instances you store in the array are not shared, you can use vector<unique_ptr<Bucket>>
. Else, if there is a shared semantics, you may want to use vector<shared_ptr<Bucket>>
. You can use vector::push_back()
method to add new instances of Bucket
's to the vector, which will dynamically grow. If you choose to use shared_ptr<>
smart pointer, allocate the instances of Bucket
's using make_shared
instead of raw operator new
.
如果存储在数组中的Bucket实例未共享,则可以使用vector
There is a very interesting series of STL lessons on Channel 9, by the STL maintainer working in Visual C++ Team. You may want to consider part 1 (sequence containers) and part 3 (smart pointers).
在Visual C ++团队中工作的STL维护者在第9频道上有一系列非常有趣的STL课程。您可能需要考虑第1部分(序列容器)和第3部分(智能指针)。
#3
1
Do not re-invent the wheel. Use std::vector<Bucket<E> >
if you want a resizeable array. If you don't want the buckets to be copied over, you will have to use some indirection. For instance with smart pointers:
不要重新发明*。如果需要可调整大小的数组,请使用std :: vector
std::vector<std::shared_ptr<Bucket<E>>> index {std::make_shared<Bucket<E>>()/*, ...*/}
#4
0
If it is an Array of pointers it will not creat the copy of object it will be only a copy of a pointer and it seems that this code does the thing you want.
如果它是一个指针数组,它将不会创建对象的副本,它将只是一个指针的副本,而且看起来这个代码可以完成你想要的东西。