[英]C++ template class reference pass by-reference
我正在研究模板類,並且在書中看到了教程代碼。 有一個名為Stack的類,如下所示:
// Stack class template.
#ifndef STACK_H
#define STACK_H
template< typename T >
class Stack
{
public:
Stack( int = 10 ); // default constructor (stack size 10)
// destructor
~Stack()
{
delete [] stackPtr; // deallocate internal space for stack
} // end ~Stack destructor
bool push( const T& ); // push an element onto the stack
bool pop( T& ); // pop an element off the stack
// determine whether Stack is empty
bool isEmpty() const
{
return top == -1;
} // end function isEmpty
// determine whether Stack is full
bool isFull() const
{
return top == size - 1;
} // end function isFull
private:
int size; // # of elements in the stack
int top; // location of the top element (-1 means empty)
T *stackPtr; // pointer to internal representation of the stack
}; // end class template Stack
// constructor template
template< class T >
Stack< T >::Stack( int s )
: size( s > 0 ? s : 10 ), // validate size
top( -1 ), // Stack initially empty
stackPtr( new T[ size ] ) // allocate memory for elements
{
// empty body
} // end Stack constructor template
// push element onto stack;
// if successful, return true; otherwise, return false
template< class T >
bool Stack< T >::push( const T &pushValue )
{
if ( !isFull() )
{
stackPtr[ ++top ] = pushValue; // place item on Stack
return true; // push successful
} // end if
return false; // push unsuccessful
} // end function template push
// pop element off stack;
// if successful, return true; otherwise, return false
template< class T >
bool Stack< T >::pop( T &popValue )
{
if ( !isEmpty() )
{
popValue = stackPtr[ top-- ]; // remove item from Stack
return true; // pop successful
} // end if
return false; // pop unsuccessful
} // end function template pop
#endif
還有一個主要的測試器功能,使用功能模板如下。
// Fig. 14.4: fig14_04.cpp
// Stack class template test program. Function main uses a
// function template to manipulate objects of type Stack< T >.
#include <iostream>
#include <string>
#include "Stack.h" // Stack class template definition
using namespace std;
// function template to manipulate Stack< T >
template< typename T >
void testStack(
Stack< T > &theStack, // reference to Stack< T >
T value, // initial value to push
T increment, // increment for subsequent values
const string stackName ) // name of the Stack< T > object
{
cout << "\nPushing elements onto " << stackName << '\n';
// push element onto Stack
while ( theStack.push( value ) )
{
cout << value << ' ';
value += increment;
} // end while
cout << "\nStack is full. Cannot push " << value
<< "\n\nPopping elements from " << stackName << '\n';
// pop elements from Stack
while ( theStack.pop( value ) )
cout << value << ' ';
cout << "\nStack is empty. Cannot pop" << endl;
} // end function template testStack
int main()
{
Stack< double > doubleStack( 5 ); // size 5
Stack< int > intStack; // default size 10
testStack( doubleStack, 1.1, 1.1, "doubleStack" );
testStack( intStack, 1, 1, "intStack" );
system("pause");
} // end main
在testStack函數中使用引用的原因可能是什么? 當我做出變更定義為
Stack< T > theStack,
程序結束后,我收到一條錯誤消息,提示“調試斷言失敗”。
Stack中沒有副本構造函數,因此當您替換時
Stack<T>&
至
Stack<T>
使用默認的ctor。
第二個副本正在銷毀已清理的指針。
delete[] stackPtr;
我認為最好的想法是在將類的實例用作函數參數時使用引用。 但是我只是想嘗試復制構造函數,並將以下代碼添加到Stack類中,所以沒有任何變化。 錯誤消息再次出現,程序結束。 什么是正確的副本構造函數?
// copy constructor template
template< typename T >
Stack< T >::Stack(const Stack &something)
{
top = something.top;
size = something.size;
stackPtr = something.stackPtr;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.