当前位置:网站首页>Copy constructor template and copy assignment operator template

Copy constructor template and copy assignment operator template

2022-07-06 10:38:00 pickled cabbage

#include <iostream>
#include <string>
using namespace std;
template <typename T1>
class A
{
    
public:
    // Constructor template 
    template <typename T2>
    A(T2 v1, T2 v2);

    // Member function template 
    template <typename T3>
    void myft(T3 tmpt) 
    {
    
        cout << tmpt << endl;
    }
    T1 m_ic;
    static constexpr int m_stcvalue = 200;
public:
    A(double v1, double v2)
    {
    
        cout << "A(double v1, double v2)" << endl;
    }
    A(T1 v1, T1 v2)
    {
    
        cout << "A(T1 v1, T1 v2)" << endl;
    }
public:
    // Copy constructor template 
    template <typename U>
    A(const A<U>& other)
    //A(A<U>& other)
    {
    
        cout << "A::A(const A<U>& other)" << endl;
    }
    // Copy assignment operator template 
    template <typename U>
    A<T1>& operator=(const A<U>& other)
        //A<T1>& operator=(A<U>& other)
    {
    
        cout << "operator=(const A<U>& other)" << endl;
        return *this;
    }
};
// Off class definition 
template <typename T1>
template <typename T2>
A<T1>::A(T2 v1, T2 v2)
{
    
    cout << "A::A(T2,T2) Yes !" << endl;
}
int main()
{
    
    A<float> a(11.1f, 12.2f);
    a.m_ic = 16.2f;
    A<float> a1(a);
    return 0;
}

// If the parameters of copy constructor template and copy assignment operator template are not const, Will not call .

If you bring it const, And make corresponding calls , So here a1 The type of needs to be similar to a Different types of , For example int.

原网站

版权声明
本文为[pickled cabbage]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131705317524.html