数组作为班级的私人成员 [英] Array as private member of class

查看:154
本文介绍了数组作为班级的私人成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有私有成员的类,该私有成员是一个数组.我不知道数组的大小,并且直到将值传递到构造函数中时才会知道.定义类构造函数以及.h文件中的定义以允许此可变大小的数组的最佳方法是什么?

I am trying to create a class which has a private member that is an array. I do not know the size of the array and will not until the value is passed into the constructor. What is the best way to go about defining the class constructor as well as the definition in the .h file to allow for this variable size of the array?

推荐答案

如果要使用真正的" C样式的数组,则必须在类中添加一个指针私有成员,并在其中为它动态分配内存.构造函数(带有 new ).显然,您一定不要忘记在析构函数中释放它.

If you want a "real" C-style array, you have to add a pointer private member to your class, and allocate dynamically the memory for it in the constructor (with new). Obviously you must not forget to free it in the destructor.

class YourClass
{
  private:
    int * array;
    size_t size;

    // Private copy constructor operator to block copying of the object, see later
    // C++03:
    YourClass(const YourClass &); // no definition
    // C++11:
    YourClass(const YourClass&) = delete;

  public:
    YourClass(size_t Size) : array(new int[Size]), size(Size)
    {
        // do extra init stuff here
    };

    ~YourClass()
    {
        delete [] array;
    }
};

为简化此工作,您可以考虑使用智能指针(例如,

To make this work easier, you may consider to use a smart pointer (for example, a boost::scoped_array in C++03, or plain std::unique_ptr in C++11), that you may initialize using the initializer list before the constructor or simply in the constructor.

class YourClass
{
  private:
    boost::scoped_array<int> array; // or in C++11 std::unique_ptr<int[]> array;
    size_t size;
  public:
    YourClass(size_t Size) : array(new int[Size]), size(Size)
    {
        // do extra init stuff here
    }

    // No need for a destructor, the scoped_array does the magic
};

这两种解决方案都产生不可复制的对象(您未指定它们是否必须可复制以及它们的复制语义);如果不必复制该类(通常会发生多次),那么两者都可以,如果您尝试将一个类复制/分配给另一个类,则编译器将生成错误,在第一种情况下,因为默认复制

Both these solutions produce noncopyable objects (you didn't specify if they had to be copyable and their copy semantic); if the class don't have to be copied (which happens most of times), these both are ok, and the compiler will generate an error if you try to copy/assign one class to another, in the first case because the default copy constructor has been overloaded with a private one (or plain deleted in C++11), in the second case because boost::scoped_array and std::unique_ptr are noncopyable.

相反,如果您要具有可复制的对象,则必须决定是否要创建共享该数组的副本(因此,仅是指针副本),或者是否要为该对象创建新的单独数组其他对象.

If, instead, you want to have copyable objects, then you must decide if you want to create a copy that shares the array (so, just a pointer copy) or if you want to create a new, separate array for the other object.

在第一种情况下,在释放分配的内存之前,您必须非常小心,因为其他对象可能正在使用它.引用计数器是最常见的解决方案.您可以通过

In the first case, you must be very careful before freeing the allocated memory, since other objects may be using it; a reference-counter is the most common solution. You can be helped in this by boost::shared_array (or std::shared_ptr in C++11), which does all the tracking work automatically for you.

相反,如果要执行深层复制",则必须分配新的内存并将源数组的所有对象复制到目标数组.正确执行此操作并非完全无关紧要,通常可以通过复制并交换成语" .

If instead you want to do a "deep copy", you'll have to allocate the new memory and copy all the objects of the source array to the target array. This is not completely trivial to do correctly, and is usually accomplished through the "copy and swap idiom".

仍然,最简单的解决方案是使用std::vector作为私有成员:它将自行处理所有分配/取消分配的内容,在构造/销毁类的对象时正确地构造/破坏自己.此外,它开箱即用地实现了深度复制语义.如果您需要使调用者以只读方式访问此引导程序,则可以编写一个getter来返回对vector对象的const_iteratorconst引用.

Still, the simplest solution is to use a std::vector as a private member: it would handle all the allocation/deallocation stuff by itself, constructing/destroying itself correctly when the object of your class is constructed/destructed. Moreover, it implements the deep-copy semantic out of the box. If you need to make your callers access the vector read-only, then, you could write a getter that returns a const_iterator or a const reference to the vector object.

这篇关于数组作为班级的私人成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆