在header中使用c++数组声明。

时间:2022-08-08 15:07:08

I was wondering if it is possible to declare an array (size not known at this time), as a private member of a class and later set the size in the constructor of the class. For example:

我想知道是否有可能将数组(此时不知道其大小)声明为类的私有成员,然后在类的构造函数中设置其大小。例如:

class Test {
int a[];
public:
Test(int size);
};

Test::Test(int size) {
a[size];   // this is wrong, but what can i do here?
}

Is this possible or should I use dynamic arrays? Thanks!

这可能吗?还是应该使用动态数组?谢谢!

7 个解决方案

#1


13  

No this is not possible. Array declarations in headers must have constant sized value. Otherwise it's impossible for constructs like "sizeof" to function properly. You'll need to declare the array as a pointer type and use new[] in the constructor. Example.

不,这是不可能的。header中的数组声明必须具有常量大小的值。否则,像“sizeof”这样的结构就不可能正常工作。您需要将数组声明为指针类型,并在构造函数中使用new[]。的例子。

class Test { 
    int *a;
public:
    Test(int size) {
       a = new int[size];
    }
    ~Test() { delete [] a; }
private:
    Test(const Test& other);
    Test& operator=(const Test& other);
};

#2


17  

Short Answer: No (The size of an array is defined at compile time only)
Long Answer:

短答案:No(数组的大小仅在编译时定义)长答案:

You can use a vector to achieve the same result:

您可以使用一个向量来实现相同的结果:

class Test
{
    std::vector<int> a;
    public:
        Test(std::size_t size):
            a(size)
        {}
};

#3


4  

As other answers have pointed out, the size of an array is fixed at compile time. However, by using templates you can parameterise the size at compile time:

正如其他答案所指出的,数组的大小在编译时是固定的。但是,通过使用模板,您可以在编译时参数化大小:

template <int N> class Test {
    int a[N];
public:
    Test() { }
};

Test<5> test;
Test<40> biggertest;

This technique does not let you compute the size at run time (as the dynamic std::vector solution does), but depending on your needs this may be sufficient.

这种技术不允许您在运行时计算大小(就像动态std::vector解决方案那样),但是根据您的需要,这可能就足够了。

#4


3  

First of all, it is generally better to initialize things in the initialization list of the constructor, not in the body of the constructor.

首先,通常最好在构造函数的初始化列表中初始化东西,而不是在构造函数的主体中。

You can only initialize an array with a predefined bound if you know that bound at compile time. In this situation, you will need to dynamically allocate the space.

如果您知道在编译时绑定,则只能初始化一个带有预定义绑定的数组。在这种情况下,您需要动态地分配空间。

You must remember then to have a destructor that would delete the array when the object is destroyed or you would get a memory leak.

你必须记得有一个析构函数,当对象被破坏或者你会有内存泄漏时,它会删除数组。

#5


2  

See Martin's solution (use std::vector), and remember that even if you need to pass a buffer to a C API std::vector lets you do it by passing &vec[0] :

请参见Martin的解决方案(使用std::vector),并且记住,即使您需要将缓冲区传递给一个C API std::vector允许您通过vec[0]:

std::vector<char> vec(10);
memset(&vec[0], 0, vec.size());

It's guaranteed to work, but only if the vector isn't empty (C++ quirks, <sigh>).

它保证可以工作,但前提是向量不是空的(c++怪癖, <叹气> )。

#6


0  

No, this is not possible. You should use a dynamic array such as an std::vector. C99 allows a struct to have an unsized array as the last member only, but even when you do this you still have to manually allocate the memory yourself, e.g. with malloc().

不,这是不可能的。您应该使用动态数组,例如std::vector。C99允许结构体只有一个未大小的数组作为最后一个成员,但是即使这样做,您仍然需要自己手动分配内存,例如使用malloc()。

#7


0  

What you're talking about is not possible. Classes always have a constant size. You could have your class use a pointer to a dynamically allocated array or you could use a std::vector.

你所说的是不可能的。类的大小总是固定的。可以让类使用指向动态分配数组的指针,也可以使用std::vector。

#1


13  

No this is not possible. Array declarations in headers must have constant sized value. Otherwise it's impossible for constructs like "sizeof" to function properly. You'll need to declare the array as a pointer type and use new[] in the constructor. Example.

不,这是不可能的。header中的数组声明必须具有常量大小的值。否则,像“sizeof”这样的结构就不可能正常工作。您需要将数组声明为指针类型,并在构造函数中使用new[]。的例子。

class Test { 
    int *a;
public:
    Test(int size) {
       a = new int[size];
    }
    ~Test() { delete [] a; }
private:
    Test(const Test& other);
    Test& operator=(const Test& other);
};

#2


17  

Short Answer: No (The size of an array is defined at compile time only)
Long Answer:

短答案:No(数组的大小仅在编译时定义)长答案:

You can use a vector to achieve the same result:

您可以使用一个向量来实现相同的结果:

class Test
{
    std::vector<int> a;
    public:
        Test(std::size_t size):
            a(size)
        {}
};

#3


4  

As other answers have pointed out, the size of an array is fixed at compile time. However, by using templates you can parameterise the size at compile time:

正如其他答案所指出的,数组的大小在编译时是固定的。但是,通过使用模板,您可以在编译时参数化大小:

template <int N> class Test {
    int a[N];
public:
    Test() { }
};

Test<5> test;
Test<40> biggertest;

This technique does not let you compute the size at run time (as the dynamic std::vector solution does), but depending on your needs this may be sufficient.

这种技术不允许您在运行时计算大小(就像动态std::vector解决方案那样),但是根据您的需要,这可能就足够了。

#4


3  

First of all, it is generally better to initialize things in the initialization list of the constructor, not in the body of the constructor.

首先,通常最好在构造函数的初始化列表中初始化东西,而不是在构造函数的主体中。

You can only initialize an array with a predefined bound if you know that bound at compile time. In this situation, you will need to dynamically allocate the space.

如果您知道在编译时绑定,则只能初始化一个带有预定义绑定的数组。在这种情况下,您需要动态地分配空间。

You must remember then to have a destructor that would delete the array when the object is destroyed or you would get a memory leak.

你必须记得有一个析构函数,当对象被破坏或者你会有内存泄漏时,它会删除数组。

#5


2  

See Martin's solution (use std::vector), and remember that even if you need to pass a buffer to a C API std::vector lets you do it by passing &vec[0] :

请参见Martin的解决方案(使用std::vector),并且记住,即使您需要将缓冲区传递给一个C API std::vector允许您通过vec[0]:

std::vector<char> vec(10);
memset(&vec[0], 0, vec.size());

It's guaranteed to work, but only if the vector isn't empty (C++ quirks, <sigh>).

它保证可以工作,但前提是向量不是空的(c++怪癖, <叹气> )。

#6


0  

No, this is not possible. You should use a dynamic array such as an std::vector. C99 allows a struct to have an unsized array as the last member only, but even when you do this you still have to manually allocate the memory yourself, e.g. with malloc().

不,这是不可能的。您应该使用动态数组,例如std::vector。C99允许结构体只有一个未大小的数组作为最后一个成员,但是即使这样做,您仍然需要自己手动分配内存,例如使用malloc()。

#7


0  

What you're talking about is not possible. Classes always have a constant size. You could have your class use a pointer to a dynamically allocated array or you could use a std::vector.

你所说的是不可能的。类的大小总是固定的。可以让类使用指向动态分配数组的指针,也可以使用std::vector。