VectorList类构造函数
对于我的作业,我们正在创建一个 VectorList 类,我有一个关于构造函数的问题。这是类声明:
#include <iostream>
#include <vector>
using namespace std;
template< typename NODETYPE >
class VectorList
{
public:
VectorList(); // constructor
~VectorList(); // destructor
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
vector< NODETYPE > *vList; // list data as a vector
};
我认为对于构造函数我会使用 vList = new std::vector;但作业有这样的:
template< typename NODETYPE >
VectorList< NODETYPE >::VectorList()
: // Fill in the missing code
{
// empty body
}
所以我不能把 vList = new std::vector;在构造函数主体中,因为他希望它为空。我不确定此时该怎么办。
for my assignment we are making a VectorList class and I had a question about the constructor. This is the class declaration:
#include <iostream>
#include <vector>
using namespace std;
template< typename NODETYPE >
class VectorList
{
public:
VectorList(); // constructor
~VectorList(); // destructor
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
vector< NODETYPE > *vList; // list data as a vector
};
I thought for the constructor I would use vList = new std::vector; but the assignment has this:
template< typename NODETYPE >
VectorList< NODETYPE >::VectorList()
: // Fill in the missing code
{
// empty body
}
So I cannot put vList = new std::vector; in the constructor body as he wants it empty. I am unsure of what to do at this point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的老师正在寻找构造函数初始值设定项列表。这使您可以在构造函数运行之前初始化成员:
这是初始化成员的标准方法,因为它相当于在普通变量的声明时初始化,例如,
如果您有一个声明为 类成员 变量code>int a;,初始化不能在声明时完成,而是在对象创建后将其放入构造函数中运行。这实际上是赋值,并且使用
operator=
而不是对象的构造函数进行。它相当于:另请注意,您在我假设的头文件中使用命名空间 std; ;这是一个坏主意,因为它会污染
#include
作为标头的任何文件的命名空间,并可能导致名称冲突(从而导致微妙的错误)。删除该行后,您必须在vector
之前添加
std::
前缀。 NODETYPE > 声明。最后,在您迄今为止发布的代码中,当您可以将常规向量对象作为成员变量时,无需在堆上分配
vList
(使用 new):这将简化您的代码并减少引入微妙的内存管理错误的可能性。您不再需要初始值设定项列表,并且析构函数也无所事事。此外,您不必担心定义自定义复制构造函数和赋值运算符以使复制和赋值安全/正常工作。
Your teacher is fishing for a constructor initializer list. This lets you initialize members before the constructor is run:
This is the standard way to initialize members since it is equivalent to initializing at declaration time for normal variables, e.g.
If you had a class member variable declared as
int a;
, the initialization can't be done at declaration, but putting it in the constructor runs after the object is already created. This is really assignment, and takes place usingoperator=
instead of the object's constructor. It is equivalent to:Note also that you are
using namespace std;
in what I assume is a header file; this is a bad idea as it will pollute the namespace of any file that#include
s your header, and could potentially cause name conflicts (and thus subtle errors). After removing that line you'll have to add thestd::
prefix before yourvector< NODETYPE >
declaration.Finally, in the code you've posted so far, there's no need to allocate
vList
on the heap (with new) when you can have a regular vector object as a member variable:This would simplify your code and reduce the possibility of introducing subtle memory-management bugs. You wouldn't need your initializer list anymore, and the destructor would also have nothing to do. Additionally, you wouldn't have to worry about defining a custom copy-constructor and assignment operator to make copy and assignment safe/work properly.