模板类中重载<<和>>操作符

本文介绍了在模板类中重载输入输出流的三种方法,包括在类内部实现、通过公共成员函数间接访问私有成员以及使用过渡函数的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在模板类中输入流">>"和输出流"<<"的重载,若使用友元在类内声明,在类外实现,那么连接时会报错。我们可以采用以下三种方式来实现输出流"<<"和输入流">>"的重载。

     《一》将输出流"<<"和输入流“>>”重载的实现写在类中。

    下面给出示例的代码:

template<typename Type>
class Test;

template<typename Type>
ostream& operator<<(ostream &out,const Test<Type> &t);

template<typename Type>
class Test
{
    friend ostream& operator<<(ostream &out,const Test<Type> &t)
    {
        cout << *(t.ptr);
        return out;
    }
public:
    Test(Type * p = 0):ptr(p)
    {}
    Test(const Test<Type> &p):ptr(p.ptr)
    {}
    ~Test()
    {
        delete ptr;
        cout << "des!" << endl;
    }
    Test<Type>& operator=(Test<Type>& t)
    {
        if(this != &t){
            delete ptr;
            ptr = t.ptr;
            t.ptr = NULL;
        }
        return *this;
    }
    Type& operator*()const
    {
        return *ptr;
    }
private:
    Type *ptr;   
};
int main(int argc,char**argv)
{
    Test<int> t(new int(10));
    Test<int> t1(new int(100));
    cout << "t = " << *t<<endl;
    cout << "t1 = " << *t1 <<endl;
    t = t1;
    
    cout << "t = " << t <<endl;
    return 0;
}




其执行结果如下:

    下面我们来说明为什么输出流的重载不能在类外实现呢?

template<typename Type>
ostream& operator<<(ostream& out,Test<Type>& t)
{
    cout << *(t.ptr);
    return out;
}

上面正是函数模板的定义,而我们知道操作符重载函数不是类的成员函数,因此此处相当于定义了一个新的函数模板(不同于类中的friend ostream& operator<<(ostream& out,Test<Type>& t))。但若去掉template<tapename Type>,函数中的参数就不知道Test<Type>是什么类型,所以不能在模板类声明,类外实现操作符重载。

    《二》既然类外实现相当于重定义了一个函数模板,那么只要它不使用类的私有成员即可,因此重载的函数模板只有通过类的公有成员函数来实现对类的私有成员的操作,这样不必在类内声明它为友元,直接在类外重载即可。

    下面给出具体的代码:

  

template<typename Type>
class Test
{
public:
    Test(Type * p = 0):ptr(p)
    {}
    Test(const Test<Type> &p):ptr(p.ptr)
    {}
    ~Test()
    {
        delete ptr;
        cout << "des!" << endl;
    }
    Test<Type>& operator=(Test<Type>& t)
    {
        if(this != &t){
            delete ptr;
            ptr = t.ptr;
            t.ptr = NULL;
        }
        return *this;
    }
    Type& operator*()const
    {
        return *ptr;
    }
public:
    Type& GetData()const
    {return *ptr;}
    void SetData(const Type*& t)
    {
        ptr = t;
    }
private:
    Type *ptr;   
};

template<typename Type>
ostream& operator<<(ostream& out,Test<Type>& t)
{
    cout << t.GetData();
    return out;
}
int main(int argc,char**argv)
{
    Test<int> t(new int(10));
    Test<int> t1(new int(100));
    cout << "t = " << *t<<endl;
    cout << "t1 = " << *t1 <<endl;
    t = t1;
    
    cout << "t = " << t <<endl;
    return 0;
}


程序的执行结果:

    《三》使用过渡函数:

下面给出具体的代码:

template<typename Type>
class Test
{
public:
    Test(Type * p = 0):ptr(p)
    {}
    Test(const Test<Type> &p):ptr(p.ptr)
    {}
    ~Test()
    {
        delete ptr;
        cout << "des!" << endl;
    }
    Test<Type>& operator=(Test<Type>& t)
    {
        if(this != &t){
            delete ptr;
            ptr = t.ptr;
            t.ptr = NULL;
        }
        return *this;
    }
    Type& operator*()const
    {
        return *ptr;
    }
public:
    //输出过渡函数
    template<typename CharT,typename CharTraits>
    basic_ostream<CharT,CharTraits>& Output(basic_ostream<CharT,CharTraits>& out)const
    {
        out << *ptr;
        return out;
    }
    //输入过渡函数
    template<typename CharT,typename CharTraits>
    basic_istream<CharT,CharTraits>& Iutput(basic_istream<CharT,CharTraits>& in)
    {
        in >> ptr;
        return in;
    }
private:
    Type *ptr;   
};
//输出流重载
template<typename Type,typename CharT,typename CharTraits>
basic_ostream<CharT,CharTraits>& operator<<(basic_ostream<CharT,CharTraits>& out,const Test<Type>& t)
{
    return t.Output(out);
}
//输入流重载
template<typename Type,typename CharT,typename CharTraits>
basic_istream<CharT,CharTraits>& operator>>(basic_istream<CharT,CharTraits>& in,Test<Type>& t)
{
    return in.Input(in);
}
int main(int argc,char**argv)
{
    Test<int> t(new int(10));
    Test<int> t1(new int(100));
    cout << "t = " << *t<<endl;
    cout << "t1 = " << *t1 <<endl;
    t = t1;
    
    cout << "t = " << t <<endl;
    return 0;
}

程序执行结果:


Set模板类是一种集合,它可以存储一组不同的元素,并且它们是按照某种规则排列的。可以通过重载+、-、<<>>操作符来实现Set模板类。在这里,我们需要定义一个模板类Set,这个类可以接受任意类型的元素作为其成员。 在定义Set类时,需要实现如下操作: 1. 实现一个构造函数,用于初始化Set对象; 2. 实现一个析构函数,用于销毁Set对象; 3. 重载+、-操作符,分别用于向Set对象中添加元素从Set对象中删除元素; 4. 重载<<>>操作符,分别用于输出输入Set对象中的元素。 下面是一个简单的Set类的代码示例: ```cpp #include <iostream> #include <set> using namespace std; template<typename T> class Set { public: Set() {} virtual ~Set() {} void add(const T& elem) { m_data.insert(elem); } void remove(const T& elem) { m_data.erase(elem); } friend Set operator+(const Set& s1, const Set& s2) { Set s = s1; for (auto it = s2.m_data.begin(); it != s2.m_data.end(); ++it) { s.add(*it); } return s; } friend Set operator-(const Set& s1, const Set& s2) { Set s = s1; for (auto it = s2.m_data.begin(); it != s2.m_data.end(); ++it) { s.remove(*it); } return s; } friend ostream& operator<<(ostream& os, const Set& s) { os << "{ "; for (auto it = s.m_data.begin(); it != s.m_data.end(); ++it) { os << *it << " "; } os << "}"; return os; } friend istream& operator>>(istream& is, Set& s) { T elem; while (is >> elem) { s.add(elem); } return is; } private: set<T> m_data; }; ``` 使用示例: ```cpp int main() { Set<int> s1, s2; s1.add(1); s1.add(2); s1.add(3); s2.add(2); s2.add(3); s2.add(4); Set<int> s3 = s1 + s2; // 并集 Set<int> s4 = s1 - s2; // 差集 cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; cout << "s1 + s2: " << s3 << endl; cout << "s1 - s2: " << s4 << endl; cin >> s1; // 输入集合元素 cout << "s1: " << s1 << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值