返回临时对象会执行析构函数
我的解决方法时在函数内部用类指针,返回类指针就不会有问题了。
不知道大家还有没有其他解决方法。
复制构造函数和重载=的区别
这里不讲述为什么要使用复制构造函数和重载=(自行百度)
C++中调用复制构造函数的三种情况:
第一种:Myclass A=B;
第二种:fun(Myclass a); 对象作为参数传递
第三种:a = fun();
1.通过一个对象构造另一个对象
2.调用参数为对象的函数
3.调用返回值为对象的函数
题目:
注意:我自己写的myExceptions.h 就不放进来了。
chainNode.h:
#pragma once
#include<iostream>
template<class T>
struct chainNode
{
T element;
chainNode<T>* next;
chainNode(){}
chainNode(const T& element,chainNode<T>*next=NULL){
this->element = element;
this->next = next;
}
};
chain.h:
#pragma once
#include"chainNode.h"
#include<sstream>
#include"myExceptions.h"
using namespace std;
template<class T>
class chain
{
public:
chain();
chain(const chain<T>&);
~chain();
bool empty()const;
int size()const;
T& get(int theIndex)const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void append(const T& theElement);
void output()const;
chain<T>& operator=(const chain<T>& set);
chain<T>*& union_set(const chain<T>& setB);//求并集
chain<T>*& inter_set(const chain<T>& setB);//求交集
chain<T>*& differ_set(const chain<T>& setB);//求差集
private:
void checkIndex(int theIndex)const;
chainNode<T>* firstNode;
chainNode<T>* TailNode;
int listSize;
};
chain.cpp:
#include "chain.h"
#include<algorithm>
template<class T>
chain<T>::chain()
{
firstNode = NULL;
TailNode = NULL;
listSize = 0;
}
template<class T>
chain<T>::chain(const chain<T>&theList)
{
listSize = theList.listSize;
if (listSize == 0) {
firstNode = NULL;
return;
}
chainNode<T>* sourseNode = theList.firstNode;
firstNode = new chainNode<T>(sourseNode->element);
sourseNode = sourseNode->next;
TailNode = firstNode;
while (sourseNode!= NULL) {
chainNode<T>* nextNode = new chainNode<T>(sourseNode->element);
TailNode->next = nextNode;
TailNode = nextNode;
sourseNode = sourseNode->next;
}
}
template<class T>
chain<T>::~chain()
{
while (firstNode != NULL) {
chainNode<T>* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
template<class T>
bool chain<T>::empty() const
{
return listSize == 0;
}
template<class T>
int chain<T>::size() const
{
return listSize;
}
template<class T>
T& chain<T>::get(int theIndex) const
{
checkIndex(theIndex);
chainNode<T>* cur = firstNode;
for (int i = 0; i < theIndex; i++) {
cur = cur->next;
}
return cur->element;
}
template<class T>
void chain<T>::erase(int theIndex)
{
checkIndex(theIndex);
chainNode<T>* deleteNode;
if (theIndex == 0) {
deleteNode = firstNode;
firstNode = firstNode->next;
TailNode = NULL;
}
else {
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++) {
p = p->next;
}
if (theIndex == listSize - 1) {
TailNode = p;
}
deleteNode = p->next;
p->next = deleteNode->next;
}
listSize--;
delete deleteNode;
}
template<class T>
void chain<T>::insert(int theIndex, const T& theElement)
{
if (theIndex < 0 || theIndex > listSize) {
ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw illegalIndex(s.str());
}
if (theIndex == 0) {
firstNode = new chainNode<T>(theElement,firstNode);
TailNode = firstNode;
}
else{
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++) {
p = p->next;
}
chainNode<T>* temp = new chainNode<T>(theElement);
temp->next = p->next;
p->next = temp;
if (theIndex == listSize) {
TailNode = temp;
}
}
listSize++;
}
template<class T>
void chain<T>::append(const T& theElement)
{
if (listSize == 0) {
firstNode = new chainNode<T>(theElement, firstNode);
TailNode = firstNode;
}
else {
chainNode<T>* temp = new chainNode<T>(theElement);
TailNode->next = temp;
TailNode = temp;
}
listSize++;
}
template<class T>
void chain<T>::output() const
{
chainNode<T>* cur = firstNode;
while (cur != NULL) {
cout << cur->element << " ";
cur = cur->next;
}
cout << "\n";
}
template<class T>
chain<T>& chain<T>::operator=(const chain<T>& set)
{
while (firstNode != NULL) {
chainNode<T>* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
listSize = set.listSize;
if (listSize == 0) {
firstNode = NULL;
return *this;
}
chainNode<T>* sourseNode = set.firstNode;
firstNode = new chainNode<T>(sourseNode->element);
sourseNode = sourseNode->next;
TailNode = firstNode;
while (sourseNode != NULL) {
chainNode<T>* nextNode = new chainNode<T>(sourseNode->element);
TailNode->next = nextNode;
TailNode = nextNode;
sourseNode = sourseNode->next;
}
return *this;
}
template<class T>
chain<T>*& chain<T>::union_set(const chain<T>& setB)
{
T* A = new T[listSize];
T* B = new T[setB.listSize];
chainNode<T>* cur1 = firstNode;
chainNode<T>* cur2 = setB.firstNode;
for (int i = 0; i < listSize; i++) {
A[i] = cur1->element;
cur1 = cur1->next;
}
for (int i = 0; i < setB.listSize; i++) {
B[i] = cur2->element;
cur2 = cur2->next;
}
sort(A, A + listSize);
sort(B, B + setB.listSize);
int p = 0, q = 0;
chain<T>* c = new chain<T>();
while (p < listSize && q < setB.listSize) {
if (A[p] <= B[q]) {
c->append(A[p]);
if (A[p] == B[q]) {
q++;
}
p++;
}
else {
c->append(B[q]);
q++;
}
}
while (p < listSize) {
c->append(A[p]);
p++;
}
while (q < setB.listSize) {
c->append(B[q]);
q++;
}
return c;
}
template<class T>
chain<T>*& chain<T>::inter_set(const chain<T>& setB)
{
T* A = new T[listSize];
T* B = new T[setB.listSize];
chainNode<T>* cur1 = firstNode;
chainNode<T>* cur2 = setB.firstNode;
for (int i = 0; i < listSize; i++) {
A[i] = cur1->element;
cur1 = cur1->next;
}
for (int i = 0; i < setB.listSize; i++) {
B[i] = cur2->element;
cur2 = cur2->next;
}
sort(A, A + listSize);
sort(B, B + setB.listSize);
int p = 0, q = 0;
chain<T>* c = new chain<T>();
while (p < listSize && q < setB.listSize) {
if (A[p] < B[q]) {
p++;
}
else if (A[p] > B[q]) {
q++;
}
else {
c->append(A[p]);
p++;
q++;
}
}
return c;
}
template<class T>
chain<T>*& chain<T>::differ_set(const chain<T>& setB)
{
T* A = new T[listSize];
T* B = new T[setB.listSize];
chainNode<T>* cur1 = firstNode;
chainNode<T>* cur2 = setB.firstNode;
for (int i = 0; i < listSize; i++) {
A[i] = cur1->element;
cur1 = cur1->next;
}
for (int i = 0; i < setB.listSize; i++) {
B[i] = cur2->element;
cur2 = cur2->next;
}
sort(A, A + listSize);
sort(B, B + setB.listSize);
int p = 0, q = 0;
chain<T>* c = new chain<T>();
while (p < listSize && q < setB.listSize) {
if (A[p] == B[q]) {
p++;
q++;
}
else {
c->append(A[p]);
p++;
}
}
return c;
}
template<class T>
void chain<T>::checkIndex(int theIndex) const
{
if (theIndex < 0 || theIndex >= listSize)
{
ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw illegalIndex(s.str());
}
}
test.cpp:
#include<iostream>
using namespace std;
#include"chain.h"
#include"chain.cpp"
int main() {
chain<int> a;
chain<int> b;
a.append(2);
a.append(4);
a.append(1);
a.append(3);
a.append(5);
b.append(2);
b.append(5);
b.append(10);
cout << "集合A中的元素有:";
a.output();
cout << "集合B中的元素有:";
b.output();
chain<int> *c = a.union_set(b);
cout << "集合A和集合B的并集中的元素有:";
c->output();
cout << "集合A和集合B的交集中的元素有:";
c = a.inter_set(b);
c->output();
cout << "集合A和集合B的差集中的元素有:";
c = a.differ_set(b);
c->output();
return 0;
}