C++ 在类中声明数组,并使用构造函数设置其大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9560900/
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
Declaring an array inside a class, and setting its size with the constructor
提问by sinθ
I haven't worked with c++ in a while, but I just started a project with it. This may not be possible, but Im trying to create a template class with an array that sets its size to the value of a constant which i'm trying to set with the constructor.
我有一段时间没有使用 c++,但我刚刚开始了一个项目。这可能是不可能的,但我试图用一个数组创建一个模板类,该数组将其大小设置为我试图用构造函数设置的常量的值。
This is the code of the constructor:
这是构造函数的代码:
Tarray(int s): start_size(s){
}
This is the code that sets the array size:
这是设置数组大小的代码:
const int start_size;
T this_array[start_size];
This is the entire file:
这是整个文件:
#ifndef TARRAY_H_
#define TARRAY_H_
template<typename T>
class Tarray {
private:
const int start_size;
T this_array[start_size];
int array_size;
public:
Tarray(int s): start_size(s){
}
~Tarray(){
delete[] this_array;
}
T & operator[](int i){
return this_array[i];
}
};
#endif /* TARRAY_H_ */
These are the errors I get:
这些是我得到的错误:
..\/template_array/Tarray.h:16:24: error: 'Tarray<T>::start_size' cannot appear in a constant-expression
..\/template_array/Tarray.h:16:34: error: 'new' cannot appear in a constant-expression
..\/template_array/Tarray.h:16:34: error: ISO C++ forbids initialization of member 'this_array' [-fpermissive]
..\/template_array/Tarray.h:16:34: error: making 'this_array' static [-fpermissive]
..\/template_array/Tarray.h: In instantiation of 'Tarray<Person>':
..\Human.cpp:17:24: instantiated from here
..\/template_array/Tarray.h:16:34: error: invalid in-class initialization of static data member of non-integral type 'Person*'
Build error occurred, build is stopped
Time consumed: 343 ms.
The error messages have been changing as I try to tweak the code, but these are the errors from this particular build.
当我尝试调整代码时,错误消息一直在变化,但这些是来自这个特定构建的错误。
Thanks for any help
谢谢你的帮助
回答by je4d
The reason you're getting compiler errors is this line:
您收到编译器错误的原因是这一行:
T this_array[start_size];
This line would make your Tarray
actually contain start_size
instances of T
. It wouldn't hold a pointer or reference to these instances - they would be part of same block of memory that contains the Tarray's other instance variables.
This would make the class' size depend on start_size, and start_size is not known at compile time. The size of any C++ class must be known at compile time, this isn't possible.
此行将使您的Tarray
实际包含start_size
的实例T
。它不会保存指向这些实例的指针或引用——它们将是包含 Tarray 的其他实例变量的同一内存块的一部分。这将使类的大小取决于 start_size,而 start_size 在编译时是未知的。在编译时必须知道任何 C++ 类的大小,这是不可能的。
There are two ways to solve this:
有两种方法可以解决这个问题:
- Allocate the array of
T
instances on the heap, using array new. This is whatstd::vector
does. Writing such a class and getting it to behave right when it's copied/moved/expanded/etc is difficult and tedious, so I'd recommend just usingstd::vector
instead. - Make the number of T instances fixed, and pass it as a template parameter
T
使用数组 new 在堆上分配实例数组。这就是std::vector
它的作用。编写这样一个类并让它在复制/移动/扩展/等时正确运行既困难又乏味,所以我建议只使用它std::vector
。- 固定T个实例的数量,并将其作为模板参数传递
i.e.:
IE:
template<typename T, std::size_t N>
class TArray
{
...
T this_array[N];
...
}
This is what std::array (C++11 only) and boost::array do. Again, I'd recommend using one of these instead of writing your own. Unless this is homework, of course...
这就是 std::array (仅限 C++11)和 boost::array 所做的。同样,我建议使用其中之一,而不是自己编写。除非这是作业,当然……
Lastly, it's worth noting that this is an error:
最后,值得注意的是,这是一个错误:
~Tarray(){
delete[] this_array;
}
this_array
wasn't allocated with new
, so you shouldn't delete
it. If the array is part of the class as it is here (rather than being separately heap-allocated and owned by the class), then it will be destroyed along with the rest of the class by default. Calling delete
is not only unnecessary, it will almost certainly cause a crash.
this_array
没有分配给new
,所以你不应该这样delete
做。如果数组是类的一部分,因为它在这里(而不是单独堆分配并由类拥有),那么默认情况下它将与类的其余部分一起销毁。调用delete
不仅没有必要,而且几乎肯定会导致崩溃。
回答by Rob?
std::vector
is precisely the tool for this job:
std::vector
正是这项工作的工具:
template<typename T>
class Tarray {
private:
std::vector<T> this_array;
public:
Tarray(int s): this_array(s){
}
~Tarray(){
}
T & operator[](int i){
return this_array[i];
}
};
回答by alarouche
The following code does something similar but not using the constructor:
以下代码执行类似但不使用构造函数的操作:
#ifndef TARRAY_H_
#define TARRAY_H_
template<int SizeT>
class Tarray {
private:
T this_array[SizeT];
public:
Tarray() {}
~Tarray() {}
T & operator[](int i){
return this_array[i];
}
};
#endif /* TARRAY_H_ */
and you can use it like this:
你可以像这样使用它:
TArray<10> myArray;
回答by kamprath
You have to create the array at run time.
您必须在运行时创建数组。
template<typename T>
class Tarray {
private:
const int start_size;
T* this_array;
int array_size;
Tarray( const Tarrat& inObj ); // no copy
public:
Tarray(int s): start_size(s), this_array( new T[s] ) {
}
~Tarray(){
delete[] this_array;
}
T & operator[](int i){
return this_array[i];
}
};
Note, for this to work, T must have a default constructor (that is, a constructor that takes no arguments).
请注意,要使其正常工作,T 必须有一个默认构造函数(即不带参数的构造函数)。
回答by Arafangion
Use std::vector instead, and make life simple for yourself. :)
改用 std::vector,让自己的生活变得简单。:)
(If you want a fixed-size array, then std::array might be a possibility, I think that's in C++11, if not, then boost probably has an implementation).
(如果你想要一个固定大小的数组,那么 std::array 可能是一种可能性,我认为那是在 C++11 中,如果不是,那么 boost 可能有一个实现)。
If you insist on having that ordinary array syntax, though, as if you were using ye-olde C, then you will need to use a template parameter, such that your template class has two arguments - one for the 'T' it already has now, and another for the array size.
但是,如果您坚持使用普通的数组语法,就像使用 ye-olde C 一样,那么您将需要使用模板参数,这样您的模板类就有两个参数——一个是它已经拥有的“T”现在,另一个用于数组大小。
You are making life especially difficult by managing that array yourself - if you feel you have to define a destructor, you really should define the copy constructor in addition to the constructor. (That's called the Rule Of The Big Three, if I recall correctly), instead, rely on RAII and avoid having to ever explicitly call operator delete or delete[] yourself.
你自己管理这个数组会让你的生活变得特别困难——如果你觉得你必须定义一个析构函数,除了构造函数之外,你真的应该定义复制构造函数。(如果我没记错的话,这就是所谓的三巨头规则),相反,依靠 RAII 并避免自己显式调用操作符 delete 或 delete[]。