简体   繁体   中英

Problem in implementing Stack using Vector

In a class say:

class X
{
     int _top;
     vector<int> _stack;
public:
     X() { } // SOME CONSTRUCTOR
};

Why does a constructor of this form works:

X( int capacity )
     : _stack( capacity ), _top( 0 ) { }

But these do not work:

1.

X( int capacity )
{ _stack( capacity );  _top=0; }

2.

X( int capacity )
     {_stack = capacity;  _top=0;}

Please Explain.

The first one works, because you're initializing _stack in initialization list, while the second form doesn't use initialization list.

Read the comments in this code to understand what is initialization list!

struct sample
{
    int a,b,c; 
    vector<int> _stack;
    sample(int capacity) : _stack(capacity), a(1), b(10), c(100) {} 
           // ^^^^^^^^^^^^^^^^^^^^^ this is called initialization list  

    sample(int capacity, int x) 
    {
       _stack(capacity); //wrong!

       a = b = c = x; // this is called assignment
       //here, inside the constructor body you cannot 
       //initialize member variables, because members has been 
       //constructed and initialized already by the time executions comes here!
    } 
};

Basically, the syntax _stack(capacity) invokes the constructor. And constructor can be invoked only when the object is constructed. Once the object is constructed, you cannot invoke the constructor. In the second form, you're trying to invoke the constructor by writing _stack(capacity) in the constructor body, but by that time _stack is already constructed, that is why your code doesn't work!

For detail on initialization list , read this FAQ:

[10.6] Should my constructors use "initialization lists" or "assignment"?

In the first form you are calling a constructor, but not the second and the third.

In 1 you are calling vector<T>::operator ()(int) , which is not defined for vector<T> .

In 2 you are assigned an int to vector<T> which is not defined either.

Also please keep in mind that std::vector<int>(size_t n) constructor doesn't just reserve memory, but rather populates the vector with n zeroes. If you need to set the capacity without actually adding any values to the vector, call vector<T>::reserve(size_t) .

And if that's not the goal in itself to implement the stack via vector , there's the std::stack container adaptor already available to use in the Standard Library.

stack<int> myStack;

Or

stack<int, vector<int> > myVectorBasedStack;

This form works, because it calls the constructors of the class members.

X( int capacity )
    : _stack( capacity ), //calls vector(int) constructor
    _top( 0 ) // calls int(int) constuctor
{ }

1.This does not work, because as soon as you are inside the constructor body, the constructors should be called using other syntax.

X( int capacity )
{ 
    _stack( capacity ); //this is not a constuctor call. But this is vector operator()(int) call. And vector does not have this operator defined.
    //the proper constructor call in this place would be _stack = vector<int>( capacity );
    _top=0;
}

You may have mixed up this with shortened form of declaration and constructor call. If you declare _stack to be vector and initialize at the same time you can write:

vector<int> _stack( capacity );

But this is just a short form of:

vector<int> _stack = vector<int>( capacity );

2.This is wrong, obviously because you cannot just assign integer to vector

X( int capacity ){ _stack = capacity;  _top=0; }

The working form uses the vector's constructor - this is permitted in the initializer list.

The first non-working looks like you try to explicitly call the vectors constructor - that's not allowed, the vector has already been constructed. The initializer list allows you to replace default construction of super classes and member variables with explicit constructor calls.

The third doesn't work becase vector doesn't implement an assignment operator taking an int. It could be modified to _stack.resize(capacity)

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