C++一个类模板的成员函数调用另一个类的模板成员函数

template<intdim,intsdim>inlinevoidIntegrationInfoBox<dim,sdim>::initialize(constFinit... template <int dim, int sdim>
inline
void
IntegrationInfoBox<dim,sdim>::initialize( const FiniteElement<dim,sdim> &el, const Mapping<dim,sdim> &mapping, const BlockInfo *block_info)
{
cell.template initialize<FEValues<dim,sdim> >(mapping, cell_flags,cell_quadrature);
}

这里是一个IntegrationInfoBox类模板的成员函数initialize(){},在此函数中对另一个模板类的对象cell进行初始化,调用了cell的成员函数initialize()(另一个initialize函数),但这个成员函数initialize是个模板成员函数,它有模板参数为<class FEVALUES>。参数cell_quadrature是以<FEVALUES::integral_dimension>为模板参数的模板类对象。

我的问题是:怎么理解上面的调用方式,即cell.template initialize<>(). 为什么调用另一个类的成员函数时要加template?
以及哪里有比较详细的教程。C++primer上好像没有提到这种类似的语法规则。
展开
 我来答
司马刀剑
高粉答主

2017-10-26 · 每个回答都超有意思的
知道顶级答主
回答量:4.6万
采纳率:93%
帮助的人:8465万
展开全部
#ifndef STACK_H
#define STACK_H

#include<cstdlib>
#include<iostream>

/**
* 栈
*/
template<typename ItemType>
class Stack
{
public:
Stack();

public:
/**
* 元素入栈
* @param item 需要入栈的元素
*/
void push(const ItemType& item);

/**
* 栈顶元素出栈
* @return 栈顶的元素
*/
ItemType pop();

/**
* 获取栈顶元素
* 栈顶元素
*/
ItemType peek() const;

/**
* 清空栈
*/
void clearStack();

private:
/**
* 栈是否为空
* @return true 栈为空;false 栈不为空
*/
bool isEmpty() const;

/**
* 栈是否为满
* @return true 栈为满;false 栈不为满
*/
bool isFull() const;

private:
/**
* 栈的大小
*/
static const int maxStackSize = 50;

private:
/**
* 栈
*/
ItemType array[maxStackSize];

/**
* 栈顶标识
*/
int top;
};

template<typename ItemType>
Stack<ItemType>::Stack() : top(-1)
{}

template<typename ItemType>
void Stack<ItemType>::push(const ItemType& item)
{
if (isFull())
{
std::cerr << "Stack overflow!" << std::endl;
exit(EXIT_FAILURE);
}
top++;
array[top] = item;
}

template<typename ItemType>
ItemType Stack<ItemType>::pop()
{
if (isEmpty())
{
std::cerr << "Attempt to pop an empty stack!" << std::endl;
exit(EXIT_FAILURE);
}
ItemType temp = array[top];
top--;

return temp;
}

template<typename ItemType>
ItemType Stack<ItemType>::peek() const
{
if (isEmpty())
{
std::cerr << "Attempt to peek an empty stack!" << std::endl;
exit(EXIT_FAILURE);
}
return array[top];
}

template<typename ItemType>
bool Stack<ItemType>::isEmpty() const
{
return (top == -1 ? true : false);
}

template<typename ItemType>
bool Stack<ItemType>::isFull() const
{
return (top == (maxStackSize - 1) ? true : false);
}

template<typename ItemType>
void Stack<ItemType>::clearStack()
{
//将所有元素弹出栈
while (!isEmpty())
{
pop();
}
}

#endif //STACK_H
追问
什么鬼
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式