简体   繁体   中英

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:

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 .

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 ).

  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.

  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.

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.

References: Default constructors - cppreference.com Copy constructors - 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."

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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