>T() does not produce a "null" value, right?
It produces a default constructed object for the type. In the case of std::string, it's an empty string.
>I wanted to assign a "null" value to the array element
Assigning "null" doesn't do anything meaningful in this case. What are you trying to accomplish?
>is there an equivalent in C++?
It depends on what you want to do. If you're worried about any of the cases where setting to "null" is useful, you've got fundamental design issues to iron out first. Personally, I don't see why it can't be as simple as this for your class:
#include <iostream>
#include <exception>
#include <stdexcept>
#include <string>
using namespace std;
template <class T, int size>
class Stack {
T arr[size];
int top;
public:
Stack(): top(0) {}
void push ( T obj );
T pop();
};
template <class T, int size>
void Stack<T,size>::push ( T obj )
{
if ( top >= size )
throw runtime_error ( "Stack is full" );
arr[top++] = obj;
}
template <class T, int size>
T Stack<T,size>::pop()
{
if ( top == 0 )
throw runtime_error ( "Stack is empty" );
return arr[--top];
}
int main()
{
Stack<string, 5> s;
try {
for ( int i = 0; i < 6; i++ )
s.push ( "hello" );
} catch ( exception& ex ) {
cerr<< ex.what() <<'\n';
}
try {
for ( int i = 0; i < 6; i++ )
cout<< s.pop() <<'\n';
} …