简体   繁体   English

C ++-Stack类的构造函数?

[英]C++ - Constructor in Stack class?

My assignment requires me to create a stack template class. 我的作业要求我创建一个堆栈模板类。 My program is working fine, I was just wondering if its necessary in this case to define the constructor since its only member is a vector. 我的程序运行良好,我只是想知道在这种情况下是否有必要定义构造函数,因为它的唯一成员是向量。 This is the code: 这是代码:

#include<iostream>
#include<vector>
using namespace std;

template <class T>
class Stack{

public:

    Stack(){}

    void push(const T &item){
        data.push_back(item);
    }

    void pop(){
        data.pop_back();
    }

    bool isEmpty(){
        return data.empty();
    }

    T getTop(){
        top = data.back();

        return(top);
    }

private:

    vector<T> data;
    T top;

};

If so, would I also need to include a copy constructor? 如果是这样,我是否还需要包括一个复制构造函数? How do I implement either if the only member is a vector? 如果唯一成员是向量,该如何实现?

correction: i also have another member, if you have noticed. 更正:如果您已经注意到,我也有另一位成员。 Question still stands, though. 问题仍然存在。

If you do not care about initializing top during the construction, then you do can do any of the following three: 如果您不关心在构造过程中初始化top ,则可以执行以下三个操作之一:

Stack(){};

or 要么

Stack(): default;

or not declaring a constructor 还是不声明构造函数

However, not declaring is a bad practice and it will create problems if you are using a copy constructor or any other constructor with parameters . 但是,不声明是一种不好的做法,如果您使用copy constructor或任何其他constructor with parameters则会造成问题。

For instance, if you created the constructor: 例如,如果您创建了构造函数:

Stack(T i)  { top = i; data.push_back(i); };

without having declared a constructor, 没有声明构造函数,

Stack<int>();

would generate a compiler error. 会产生编译器错误。

Just make sure that you set the value of top when push function is called (ie compare the new element with the current top every time you push, but if data.size() == 1 you should set this as a top without comparing, as top is undefined ). 只需确保在调用push函数时设置top的值data.size() == 1 ,每次按下时将新元素与当前top进行比较,但是如果data.size() == 1 ,则应将其设置为top而不进行比较,因为top is undefined )。

  1. You don't need to declare a default constructor, compiler will generate one for you - unless class T hasn't defined one, on which case you might get an error. 您无需声明默认的构造函数,编译器会为您生成一个默认的构造函数-除非类T尚未定义一个构造函数,否则可能会出现错误。

  2. You don't need a copy constructor also, for the same reasons, given the same premises - on this case, class T must have one. 出于相同的原因,在相同的前提下,您也不需要复制构造函数-在这种情况下,类T必须具有一个。

You'll find detailed info on references below. 您可以在下面找到有关参考的详细信息。

That been said, I see no reason for the extra "top" member, except for some sort of speed optimization. 话虽这么说,除了某种速度优化之外,我没有理由再需要额外的“ top”成员。

References: Default constructors - cppreference.com Copy constructors - cppreference.com 参考: 默认构造函数-cppreference.com 复制构造函数-cppreference.com

First of all, there is no need to define constructor and copy constructor. 首先,不需要定义构造函数和复制构造函数。

If we do not define a constructor and copy constructor, the compiler will synthesizes one, and the synthesized constructor will help us to initialize class member variable. 如果我们没有定义构造函数和复制构造函数,则编译器将合成一个,并且合成的构造函数将帮助我们初始化类成员变量。 You does not need to initialize any member variable with a special value in your class. 您无需在类中使用任何特殊值初始化任何成员变量。

How to define copy control? 如何定义复制控件? From C++ Primer: "There are three basic operations to control copies of class objects: the copy constructor, copy-assignment operator, and destructor." 在C ++ Primer中:“有三种基本操作来控制类对象的副本:副本构造函数,副本分配运算符和析构函数。”

Classes That Need Destructors Need Copy and Assignment: One rule of thumb to use when you decide whether a class needs to define its own versions of the copy-control members is to decide first whether the class needs a destructor. 需要析构函数的类需要复制和分配:在决定一个类是否需要定义其自己的复制控制成员版本时,要使用的一条经验法则是首先确定该类是否需要析构函数。 Often, the need for a destructor is more obvious than the need for the copy constructor or assignment operator. 通常,对析构函数的需求比对复制构造函数或赋值运算符的需求更为明显。 If the class needs a destructor, it almost surely needs a copy constructor and copy-assignment operator as well. 如果该类需要一个析构函数,则几乎肯定需要一个拷贝构造函数和一个拷贝赋值运算符。

Example: 例:

class Int
{
public:
    Int(int vValue = 0) : m_Pointer(new int(vValue)) {}
    ~Int()
    { 
        delete m_Pointer;
        m_Pointer = NULL; 
    }

private:
    int* m_Pointer;
};

We defined an int Wrapper Class like JAVA. 我们定义了一个像JAVA这样的int包装器类。 This class allocates dynamic memory in its constructor, The synthesized destructor will not delete a data member that is a pointer. 此类在其构造函数中分配动态内存,合成的析构函数将不会删除作为指针的数据成员。 Therefore, this class needs to define a destructor to free the memory allocated by its constructor. 因此,此类需要定义一个析构函数以释放其构造函数分配的内存。

Unfortunately, we have introduced a serious bug! 不幸的是,我们引入了一个严重的错误! This version of the class uses the synthesized versions of copy and assignment. 该类的版本使用复制和赋值的综合版本。 Those functions copy the pointer member, just only copy the address where the pointer member points to: 这些函数复制指针成员,仅复制指针成员指向的地址:

Int(const Int& vInt)
{
    m_Pointer = vInt.m_Pointer;
}

So, you must define your own copy constructor and assignment operator to control the memory assignment. 因此,您必须定义自己的副本构造函数和赋值运算符来控制内存分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM