VectorList类构造函数

发布于 2024-10-21 21:52:51 字数 863 浏览 2 评论 0原文

对于我的作业,我们正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-10-28 21:52:51

您的老师正在寻找构造函数初始值设定项列表。这使您可以在构造函数运行之前初始化成员:

template< typename NODETYPE > 
VectorList< NODETYPE >::VectorList()  
    : vList(new std::vector)
{
}  

这是初始化成员的标准方法,因为它相当于在普通变量的声明时初始化,例如,

int a(3);    // Calls int constructor with 3. Note int a = 3; does same (no call to operator=)

如果您有一个声明为 类成员 变量code>int a;,初始化不能在声明时完成,而是在对象创建后将其放入构造函数中运行。这实际上是赋值,并且使用 operator= 而不是对象的构造函数进行。它相当于:

int a;    // Create an int
a = 3;    // Uses operator= to do assignment

另请注意,您在我假设的头文件中使用命名空间 std; ;这是一个坏主意,因为它会污染 #include 作为标头的任何文件的命名空间,并可能导致名称冲突(从而导致微妙的错误)。删除该行后,您必须在 vector之前添加 std:: 前缀。 NODETYPE > 声明。

最后,在您迄今为止发布的代码中,当您可以将常规向量对象作为成员变量时,无需在堆上分配 vList (使用 new):

vector< NODETYPE > vList;

这将简化您的代码并减少引入微妙的内存管理错误的可能性。您不再需要初始值设定项列表,并且析构函数也无所事事。此外,您不必担心定义自定义复制构造函数和赋值运算符以使复制和赋值安全/正常工作。

Your teacher is fishing for a constructor initializer list. This lets you initialize members before the constructor is run:

template< typename NODETYPE > 
VectorList< NODETYPE >::VectorList()  
    : vList(new std::vector)
{
}  

This is the standard way to initialize members since it is equivalent to initializing at declaration time for normal variables, e.g.

int a(3);    // Calls int constructor with 3. Note int a = 3; does same (no call to operator=)

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 using operator= instead of the object's constructor. It is equivalent to:

int a;    // Create an int
a = 3;    // Uses operator= to do assignment

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 #includes your header, and could potentially cause name conflicts (and thus subtle errors). After removing that line you'll have to add the std:: prefix before your vector< 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:

vector< NODETYPE > vList;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文