在打字机t上模板时,t()是什么意思

When templating on typename T, what does T() mean?

本文关键字:是什么 意思 打字机      更新时间:2023-10-16

这是我对C 课程的分配。当然,我会自己完成。但是有一个我真的没有想法。希望有人可以向我解释。预先感谢。

此任务的要求就是这样:

我们需要使用模板创建一个定制的类,名为 set 来实现联合(使用操作员+)和交叉点(使用操作员*)的功能。例如,{4,2,3}+{9,4,8,2}将为{8,3,9,2,4},{3,2,4}*{8,4,9,2}将为{4,2}

这是xxxx.h文件的骨架:

xxxx.h文件

这是代码,但不能像图片一样显示红色矩形。但是它更可读:)

#ifndef A2P2_H
#define A2P2_H
#include <exception>
#include <iostream>
using namespace std;
//========part a-Comments here:
//========part b-author's details
void info(){/* missing code */}
//========part c-exception classes:
class RemoveFromEmpty : exception{
    public:
        RemoveFromEmpty(){/* missing code */}
        const char* what() const noexcept {/* missing code */}
    private:
        string mMessage;
};
class NonExistingElem:exception{
    /* to be thrown when the element to be removed is not found in
    the set --------code missing */
};

//========part d-Set class template
template <typename EType>
class Set{
    public:
        //constructors
        Set( );
        Set( const Set & rhs );
        Set( Set && rhs );
        //destructor
        ~Set( );
        //operators overloaded
        Set & operator=( const Set & rhs );
        Set & operator=( Set && rhs );
        Set operator+( const Set & rhs ) const; //set union
        Set operator*( const Set & rhs ) const; //set intersection
        //methods
        bool isElement( const EType & x ) const;
        bool isEmpty( ) const;
        int getSize( ) const;
        //display on out all elements in the set between {..}
        void print( ostream & out = cout ) const;
        void setToEmptySet( );
        //methods to work with individual elements of a set
        void insert( const EType & x );
        void remove( const EType & x );
    private:
        struct Node{// type of the elements of the set
            EType mData;
            Node *mNext;
            Node( const EType & d = EType( ), Node *n = nullptr )
            : mData( d ), mNext( n ) { }
        };
        Node *mFirst;
        int mSize; // to have an efficient getSize().
};
//Write the definitions of all Set function members here:
//========part e-the output operator:
template <typename EType>
ostream & operator<< /* code missing here */
#endif

请参阅上面的图片,并更多地关注突出显示的部分。在下部,有一个红色矩形,内容为 const EType &d = EType()。我知道ETYPE是我在模板中声明的类型。但是什么是EType()?这是一个函数?

在要求的其余部分中,没有其他与EType()相关的(即,也许是该功能的某些实现)。实际上,左侧是xxxx.cpp文件,执行该代码时的结果。如果有帮助,我也会在这里发布它们。

xxxx.cpp和结果

#include <iostream>
#include "a2p2.h"
using namespace std;
//It works only for sets of integers
template <typename T = int>
void testCopyCtr(Set<T> st){
    cout<< func <<": ";
    st.insert(23); //because of this statement
    st.print();
    cout<<endl;
}
int main( ){
    info(); //authors details
    try{
        Set<int> s1, s2;
        s1.insert( 8 );
        s1.insert( 3 );
        s1.insert( 1 );
        s1.insert( 4 );
        s1.insert( 1 );
        s1.remove( 8 );
        s1.insert( 2 );
        s2.insert( 4 );
        s2.insert( 2 );
        s2.insert( 6 );
        cout << "S1: " << s1 << ", size= " << s1.getSize( ) << endl;
        cout << "S2: " << s2 << ", size= " << s2.getSize( ) << endl;
        Set<int> s3 = s1+ s2; //union
        Set<int> s4 = s1* s2; //intersection
        cout << "s1 + s2: " << s3 << endl;
        cout << "s1 * s2: " << s4 << endl;
        Set<int> s5 = s4 = s3 = s2 = s1 = s1;
        cout << "S1: " << s1 << ", size= " << s1.getSize( ) << endl;
        cout << "S2: " << s2 << ", size= " << s2.getSize( ) << endl;
        cout << "S3: " << s3 << ", size= " << s3.getSize( ) << endl;
        cout << "S4: " << s4 << ", size= " << s4.getSize( ) << endl;
        cout << "S5: " << s5 << ", size= " << s5.getSize( ) << endl;
        cout << "S4 again : " << s4 << endl;
        testCopyCtr(s4);
        Set<float> sf;
        sf.remove(3);
    }
    catch(RemoveFromEmpty ex) {
        cout<<endl<<ex.what()<<endl;
        cout<<"Nothing to be donen";
    }
    catch(NonExistingElem ex) {
        cout<<endl<<ex.what()<<endl;
        cout<<"Nothing to be donen";
    }
    cout<<"All is well when it ends well!n";
    return 0;
}

不便之处,

结果只能在图片中显示。

它是构造函数的调用。您将默认结构的EType值分配给变量d作为d的默认值。