通过一个相对基本的C ++模板

时间:2014-08-02 03:22:01

标签: c++ templates set const walkthrough

我们的导师要求我们浏览模板并解释它的各个部分:

Template<class T> set<T> set<T>:: setDifference(const Set<T> &that) const

这是我到目前为止所得到的:

Template<class T>: declares the new template

set<T>: states the return type of our template

set<T>: pretty confused about the second set, possibly the class name?

setDifference: Calls on our setDifference function

const Set<T> &that: 
Parameters of setDifference, states set<T> cannot be modified within                          
setDifference or put on the left hand side of the equation. "&that" references "that"     
memory location to use/call

const (at the end): 
Our function can only be called by a const object of the class
nor can it call any non-const member functions or change member variables. 

如果有人能够纠正/添加我已经拥有的东西,我将非常感激。

1 个答案:

答案 0 :(得分:0)

这是在其类主体

之外定义成员函数setDifference的开始
template <class T>
struct set{
//...
    set setDifference( const set& that) const;
};
template<class T>
set<T> set<T>::setDifference( const set<T>& that) const
{
    set<T> newInstance (...);
    //implementation of setDifference: newInstance = this - that
    return newInstance;
}
  • 返回类型是新创建的集合类
  • 的实例
  • 第二集确实是班级名称;需要因为setDifference位于命名空间set<T>
  • 你对其他人的看法是正确的