模板运算符重载函数

运算符重载+



template <typename T, typename T1>
class People;

template <typename T, typename T1>
void myprint(const People<T, T1>& pn);
template <typename T, typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People() : age(0) {}  // Default constructor
  People(int a) : age(a) {}

  //运算符重载+ -
  People<T, T1> operator+(People<T, T1>& other);

  friend void myprint<T, T1>(const People<T, T1>& pn);
};
template <typename T, typename T1>
People<T, T1> People<T, T1>::operator+(People<T, T1>& other) {
  cout << "operator+ :" << endl;
  People<T, T1> tmp;
  tmp.age = this->age + other.age;
  return tmp;
}

template <typename T, typename T1>
void myprint(const People<T, T1>& pn) {
  cout << "a friend fun" << endl;
  cout << "pn.age=" << pn.age << endl;
}

void test070802(void) {
  People<int, int> p1(1);
  myprint(p1);

  People<int, int> p2(11);
  myprint(p2);

  People<int, int> p3(77);
  p3 = p1 + p2;

  myprint(p3);
}

运算符重载+=

template <typename T, typename T1>
class People;

template <typename T, typename T1>
void myprint(const People<T, T1>& pn);
template <typename T, typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People() : age(0) {}  // Default constructor
  People(int a) : age(a) {}

  //运算符重载+
  People<T, T1> operator+(People<T, T1>& other);

  //运算符重载 +=
  People<T, T1> operator+=(People<T, T1>& other);

  friend void myprint<T, T1>(const People<T, T1>& pn);
};
template <typename T, typename T1>
People<T, T1> People<T, T1>::operator+=(People<T, T1>& other) {
  cout << "operator+= :" << endl;
  this->age = this->age + other.age;
  return *this;
}

template <typename T, typename T1>
People<T, T1> People<T, T1>::operator+(People<T, T1>& other) {
  cout << "operator+ :" << endl;
  People<T, T1> tmp;
  tmp.age = this->age + other.age;
  return tmp;
}

template <typename T, typename T1>
void myprint(const People<T, T1>& pn) {
  cout << "a friend fun" << endl;
  cout << "pn.age=" << pn.age << endl;
}

void test070802(void) {
  People<int, int> p1(1);
  myprint(p1);

  People<int, int> p2(11);
  myprint(p2);

  People<int, int> p3(77);
  p3 = p1 + p2;

  myprint(p3);

  People<int, int> p4(99);
  p4 += p1;

  myprint(p4);
}

模板友元运算符重载实现

+作为友元运算符重载
(1)友元函数在class内实现,ok,但是因为是友元实现的,所以参数要有2个

template <typename T, typename T1>
class People;

template <typename T, typename T1>
void myprint(const People<T, T1>& pn);
template <typename T, typename T1>
class People {
private:
/* data */
int age;

public:
People() : age(0) {} // Default constructor
People(int a) : age(a) {}

//运算符重载+
friend People<T, T1> operator+(People<T, T1>& a, const People<T, T1>& b) {
cout << “friend operator+ :” << endl;
People<T, T1> tmp;
tmp.age = a.age + b.age;
return tmp;
}

//运算符重载 +=
People<T, T1> operator+=(People<T, T1>& other);

friend void myprint<T, T1>(const People<T, T1>& pn);
};
template <typename T, typename T1>
People<T, T1> People<T, T1>::operator+=(People<T, T1>& other) {
cout << “operator+= :” << endl;
this->age = this->age + other.age;
return *this;
}

template <typename T, typename T1>
void myprint(const People<T, T1>& pn) {
cout << “a friend fun” << endl;
cout << “pn.age=” << pn.age << endl;
}

void test070802(void) {
People<int, int> p1(1);
myprint(p1);

People<int, int> p2(11);
myprint(p2);

People<int, int> p3(77);
p3 = p1 + p2;

myprint(p3);
}

(2)友元函数在class外实现,OK



template <typename T, typename T1>
class People;

template <typename T, typename T1>
void myprint(const People<T, T1>& pn);

template <typename T, typename T1>
People<T, T1> operator+(People<T, T1>& a, People<T, T1>& b);


template <typename T, typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People() : age(0) {}  // Default constructor
  People(int a) : age(a) {}

  //运算符重载+
  friend People<T, T1> operator+<T, T1>(People<T, T1>& a, People<T, T1>& b);

  //运算符重载 +=
  People<T, T1> operator+=(People<T, T1>& other);

  friend void myprint<T, T1>(const People<T, T1>& pn);
};


// Define the friend operator+ within the header file
template <typename T, typename T1>
People<T, T1> operator+(People<T, T1>& a, People<T, T1>& b) {
  cout << "friend operator+ :" << endl;
  People<T, T1> tmp;
  tmp.age = a.age + b.age;
  return tmp;
}

template <typename T, typename T1>
People<T, T1> People<T, T1>::operator+=(People<T, T1>& other) {
  cout << "operator+= :" << endl;
  this->age = this->age + other.age;
  return *this;
}

template <typename T, typename T1>
void myprint(const People<T, T1>& pn) {
  cout << "a friend fun" << endl;
  cout << "pn.age=" << pn.age << endl;
}

void test070802(void) {
  People<int, int> p1(1);
  myprint(p1);

  People<int, int> p2(11);
  myprint(p2);

  People<int, int> p3(77);
  p3 = p1 + p2;

  myprint(p3);
}

(3)友元函数的第三种实现,ok

template <typename T, typename T1>
class People;

template <typename T, typename T1>
void myprint(const People<T, T1>& pn);




template <typename T, typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People() : age(0) {}  // Default constructor
  People(int a) : age(a) {}

  //运算符重载+
 template <typename A, typename A1> friend People<A, A1> operator+(People<A, A1>& a, People<A, A1>& b);

  //运算符重载 +=
  People<T, T1> operator+=(People<T, T1>& other);

  friend void myprint<T, T1>(const People<T, T1>& pn);
};


// Define the friend operator+ within the header file
template <typename T, typename T1>
People<T, T1> operator+(People<T, T1>& a, People<T, T1>& b) {
  cout << "friend operator+ :" << endl;
  People<T, T1> tmp;
  tmp.age = a.age + b.age;
  return tmp;
}

template <typename T, typename T1>
People<T, T1> People<T, T1>::operator+=(People<T, T1>& other) {
  cout << "operator+= :" << endl;
  this->age = this->age + other.age;
  return *this;
}

template <typename T, typename T1>
void myprint(const People<T, T1>& pn) {
  cout << "a friend fun" << endl;
  cout << "pn.age=" << pn.age << endl;
}

void test070802(void) {
  People<int, int> p1(1);
  myprint(p1);

  People<int, int> p2(11);
  myprint(p2);

  People<int, int> p3(77);
  p3 = p1 + p2;

  myprint(p3);
}

+=作为友元运算符重载
(1)友元函数在class内实现,ok,但必须带2个参数,带1个不行



template <typename T, typename T1>
class People;

template <typename T, typename T1>
void myprint(const People<T, T1>& pn);

template <typename T, typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People() : age(0) {}  // Default constructor
  People(int a) : age(a) {}

  //运算符重载+
  template <typename A, typename A1>
  friend People<A, A1> operator+(People<A, A1>& a, People<A, A1>& b);

  //运算符重载 +=
  friend People<T, T1> operator+=(People<T, T1>& a, People<T, T1>& b) {
    cout << "friend operator+= :" << endl;
    a.age = a.age + b.age;
    return a;
  }

  friend void myprint<T, T1>(const People<T, T1>& pn);
};

// Define the friend operator+ within the header file
template <typename T, typename T1>
People<T, T1> operator+(People<T, T1>& a, People<T, T1>& b) {
  cout << "friend operator+ :" << endl;
  People<T, T1> tmp;
  tmp.age = a.age + b.age;
  return tmp;
}

template <typename T, typename T1>
void myprint(const People<T, T1>& pn) {
  cout << "a friend fun" << endl;
  cout << "pn.age=" << pn.age << endl;
}

void test070802(void) {
  People<int, int> p1(1);
  myprint(p1);

  People<int, int> p2(11);
  myprint(p2);

  People<int, int> p3(77);
  p3 += p1;

  myprint(p3);
}

(2)友元函数在class外实现,ok



template <typename T, typename T1>
class People;

template <typename T, typename T1>
void myprint(const People<T, T1>& pn);

template <typename T, typename T1>
People<T, T1> operator+=(People<T, T1>& a, People<T, T1>& b);

template <typename T, typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People() : age(0) {}  // Default constructor
  People(int a) : age(a) {}

  //运算符重载+
  template <typename A, typename A1>
  friend People<A, A1> operator+(People<A, A1>& a, People<A, A1>& b);

  //运算符重载 +=
  friend People<T, T1> operator+=<T, T1>(People<T, T1>& a, People<T, T1>& b);

  friend void myprint<T, T1>(const People<T, T1>& pn);
};
//运算符重载 +=
template <typename T, typename T1>
People<T, T1> operator+=(People<T, T1>& a, People<T, T1>& b) {
  cout << "friend operator+= :" << endl;
  a.age = a.age + b.age;
  return a;
}
// Define the friend operator+ within the header file
template <typename T, typename T1>
People<T, T1> operator+(People<T, T1>& a, People<T, T1>& b) {
  cout << "friend operator+ :" << endl;
  People<T, T1> tmp;
  tmp.age = a.age + b.age;
  return tmp;
}

template <typename T, typename T1>
void myprint(const People<T, T1>& pn) {
  cout << "a friend fun" << endl;
  cout << "pn.age=" << pn.age << endl;
}

void test070802(void) {
  People<int, int> p1(1);
  myprint(p1);

  People<int, int> p2(11);
  myprint(p2);

  People<int, int> p3(77);
  p3 += p1;

  myprint(p3);
}

(3)友元函数的第三种实现,ok



template <typename T, typename T1>
class People;

template <typename T, typename T1>
void myprint(const People<T, T1>& pn);



template <typename T, typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People() : age(0) {}  // Default constructor
  People(int a) : age(a) {}

  //运算符重载+
  template <typename A, typename A1>
  friend People<A, A1> operator+(People<A, A1>& a, People<A, A1>& b);

  //运算符重载 +=
  template <typename B, typename B1> friend People<B, B1> operator+=(People<B, B1>& a, People<B, B1>& b);

  friend void myprint<T, T1>(const People<T, T1>& pn);
};
//运算符重载 +=
template <typename T, typename T1>
People<T, T1> operator+=(People<T, T1>& a, People<T, T1>& b) {
  cout << "friend operator+= :" << endl;
  a.age = a.age + b.age;
  return a;
}
// Define the friend operator+ within the header file
template <typename T, typename T1>
People<T, T1> operator+(People<T, T1>& a, People<T, T1>& b) {
  cout << "friend operator+ :" << endl;
  People<T, T1> tmp;
  tmp.age = a.age + b.age;
  return tmp;
}

template <typename T, typename T1>
void myprint(const People<T, T1>& pn) {
  cout << "a friend fun" << endl;
  cout << "pn.age=" << pn.age << endl;
}

void test070802(void) {
  People<int, int> p1(1);
  myprint(p1);

  People<int, int> p2(11);
  myprint(p2);

  People<int, int> p3(77);
  p3 += p1;

  myprint(p3);
}

学习记录,侵权联系删除。
来源:朱老师物联网大课堂

07-23 00:54