So I'm unsure as to what the correct way is to do this. I have a class named someClass, with a private variable that is an array of integers. The size doesn't get defined until the constructor is called. Here's how I do it:
所以我不确定正确的做法是什么。我有一个名为someClass的类,它的私有变量是一个整数数组。在调用构造函数之前,不会定义大小。我是这样做的:
In someClass.h:
在someClass.h:
class someClass {
public:
someClass();
someClass(int size);
~someClass();
private:
int* array;
}
In someClass.cpp:
在someClass.cpp:
someClass::someClass() {
array = new int[9];
}
someClass::someClass(int range) {
array = new int[range];
}
someClass::~someClass() {
delete[] array;
}
Did I declare/define the array correctly? Would it have been much better to use a vector?
我是否正确地声明/定义了数组?使用向量会更好吗?
Is the destructor correct?
析构函数是正确的?
2 个解决方案
#1
2
Yes, you're doing it correctly (partially), and yes, it would be better to use a std::vector
(because of the "partially" part).
是的,您的操作是正确的(部分),是的,最好使用std::vector(因为“部分”部分)。
The "partially" is that now you will have to provide a copy constructor and copy assignment operator for your class (and for efficiency, you might want to have a move constructor and move assignment operator as well); if you don't, you'll get double deletion errors if you ever copy the class.
“部分”是现在您必须为您的类提供一个复制构造函数和复制分配操作符(为了提高效率,您可能还需要一个移动构造函数和移动分配操作符);如果您不这样做,那么如果您复制类,您将会得到双重删除错误。
std::vector
encapsulates all this, so this is why you should prefer it. Not to mention that it has nice features like iterators.
向量包含了所有这些,所以这就是为什么您应该更喜欢它。更不用说它有像迭代器这样的好特性。
Your class would then look like this:
你的班级会是这样的:
class someClass {
public:
someClass();
someClass(int size);
//no destructor necessary!
private:
std::vector<int> array;
}
someClass::someClass()
{} // nothing special needed
someClass::someClass(int range) : array(range)
{}
#2
2
You should also add copy ctor and copy assignment operator. Remember the rule of three!
您还应该添加copy ctor和copy赋值操作符。记住三原则!
#1
2
Yes, you're doing it correctly (partially), and yes, it would be better to use a std::vector
(because of the "partially" part).
是的,您的操作是正确的(部分),是的,最好使用std::vector(因为“部分”部分)。
The "partially" is that now you will have to provide a copy constructor and copy assignment operator for your class (and for efficiency, you might want to have a move constructor and move assignment operator as well); if you don't, you'll get double deletion errors if you ever copy the class.
“部分”是现在您必须为您的类提供一个复制构造函数和复制分配操作符(为了提高效率,您可能还需要一个移动构造函数和移动分配操作符);如果您不这样做,那么如果您复制类,您将会得到双重删除错误。
std::vector
encapsulates all this, so this is why you should prefer it. Not to mention that it has nice features like iterators.
向量包含了所有这些,所以这就是为什么您应该更喜欢它。更不用说它有像迭代器这样的好特性。
Your class would then look like this:
你的班级会是这样的:
class someClass {
public:
someClass();
someClass(int size);
//no destructor necessary!
private:
std::vector<int> array;
}
someClass::someClass()
{} // nothing special needed
someClass::someClass(int range) : array(range)
{}
#2
2
You should also add copy ctor and copy assignment operator. Remember the rule of three!
您还应该添加copy ctor和copy赋值操作符。记住三原则!