简体   繁体   English

在类中定义数组,但大小由构造函数确定

[英]Defining an array in a class but the size is determined in the constructor

How can I define an array as a member of class while its size determined somewhere else and actually it is passed to the constructor. 我如何将数组定义为类的成员,而数组的大小在其他地方确定,并实际上将其传递给构造函数。

More detail: There is an array of integer numbers and is defined a public member in class. 更多详细信息:有一个整数数组,并在类中定义为公共成员。

class foo {
  public:
    int *arr[];
    int s;
};

However the size of array is passed in the constructor. 但是,数组的大小在构造函数中传递。

foo::foo()
        : s (array_size)
{
}

What is the correct syntax of doing such thing? 这样做的正确语法是什么?

The correct way of doing this is to use the STL and std::vector< int > for such tasks. 正确的方法是将STL和std::vector< int >用于此类任务。 The following is a rough outline that may work for you: 以下是可能适合您的粗略概述:

#include <vector>

...

class foo
{
    public:
        std::vector< int > arr_;

        ...

        foo(const int* numbers, int numberCount)
        : arr_(numbers, numbers+numberCount)
        {
        }

        ...

        int size() const
        {
            return arr_.size();
        }

        ...

        int& operator [] (int index)
        {
           return arr_.at(index);
        }
};

Further information on vectors can be found here . 有关向量的更多信息,请参见此处 Further suggestions: 进一步建议:

  • don't make your instance variables public if there is no compelling reason to do so. 如果没有令人信服的理由,请勿将您的实例变量公开。
  • name instance variables somehow special (for example by appending a _ ). 名称实例变量有些特殊(例如,通过附加_ )。
  • many people dislike classes that are named all lowercase. 许多人不喜欢被全部命名为小写的类。

Your class appears to be attempting to define an array of 'pointers to int ', not an array of int as you suggest. 您的班级似乎正在尝试定义“指向int的指针”的数组,而不是您建议的int数组。 However, the classic answer is precisely that you use a 'pointer to int ' and allocate the array in the constructor and release it in the destructor. 然而,经典的答案恰恰是您使用了一个“指向int的指针”,并在构造函数中分配了数组,然后在析构函数中释放了它。 To a first approximation: 初步近似:

class foo
{
public:
    int *arr;
    int  s;
    foo(int sz) : s(sz) { arr = new int [s]; }
   ~foo()               { delete [] arr; }
};

If you're going to go down this route, you will also need to provide an assignment operator and a copy constructor (as Mike Seymour reminds me - thanks, Mike); 如果您打算沿这条路线走,还需要提供一个赋值运算符和一个副本构造函数(正如Mike Seymour提醒我的那样-谢谢Mike); the default versions the compiler will write for you if you don't write them for yourself will be wrong - horribly wrong. 如果您不自己编写,编译器将为您编写的默认版本将是错误的-极其错误。 (The SO question "What is the Rule of Three?" covers this.) (SO问题“三个规则是什么?”涵盖了这一点。)

However, this is (probably) not exception safe, and you'd be well advised to use std::vector<int> instead of a plain pointer: 但是,这(可能)不是异常安全的,因此建议您使用std::vector<int>代替普通指针:

class foo
{
public:
    std::vector<int> arr;
    foo(int sz) : arr(sz) { }
};

You don't need to store the size explicitly; 您无需显式存储大小; the vector does that for you. 向量为您做到了。

Well first off, not sure why you're using an array of int pointers, but who am I to question your design.. Anywho, you can do this with a pointer and dynamic allocation. 首先,不知道为什么要使用int指针数组,但我是谁来质疑您的设计呢。任何人,都可以使用指针和动态分配来做到这一点。 Declare your array as... 将数组声明为...

int **arr;

and initialize it in the ctor as.. 并在ctor中将其初始化为

*arr = new int*[s];

Remember to clean it up on the dtor with delete[] . 请记住使用delete[]在dtor上清理它。 Another alternative is to use std::vector<int*> 另一种选择是使用std::vector<int*>

Maybe this is not exactly what you want, but I would do it with a template parameter 也许这并不是您想要的,但是我可以使用模板参数来实现

template<int T>
class Foo
{
    public:
    int array[T];
    int s;
};

You instantiate this class like 您可以像这样实例化此类

Foo<1024> * f = new Foo<1024> ();
class foo {
   public:
     std::vector m_vec;
     foo(uint32_t size) : m_vec(size) { }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM