简体   繁体   English

C ++模板类引用按引用传递

[英]C++ template class reference pass by-reference

i was studying template classes andi have seen a tutorial code in a book. 我正在研究模板类,并且在书中看到了教程代码。 There is a class named Stack as the following: 有一个名为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

Also there is a main tester function that uses a function template as following. 还有一个主要的测试器功能,使用功能模板如下。

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

What could be reason usage of reference in testStack function? 在testStack函数中使用引用的原因可能是什么? When i make change denition as 当我做出变更定义为

Stack< T > theStack,

i get an error message that says "debug assertion failed" after program ends. 程序结束后,我收到一条错误消息,提示“调试断言失败”。

There is no copy constructor in Stack, so when you replace Stack中没有副本构造函数,因此当您替换时

Stack<T>& 

to

Stack<T> 

the default ctor is used. 使用默认的ctor。

The second copy is destroying already cleaned pointer. 第二个副本正在销毁已清理的指针。

delete[] stackPtr; 

i think the best think is using reference when using a class's instance as a function parameter. 我认为最好的想法是在将类的实例用作函数参数时使用引用。 But i just wanted to try copy constructor and added following code into Stack class and nothing changed. 但是我只是想尝试复制构造函数,并将以下代码添加到Stack类中,所以没有任何变化。 Error message appeared again with program end. 错误消息再次出现,程序结束。 What could be the correct copy constructor function? 什么是正确的副本构造函数?

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

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