首页 > C++拷贝函数问题

C++拷贝函数问题

//node.h

include

using namespace std;
class Node
{
public:
Node();
Node(Node &n);
Node(int i,char c='0');
Node(int i,char c,Node *p,Node *n);
~Node();
int readi() const;
char readc() const;
Node * readp() const;
Node * readn() const;
bool set(int i);
bool set(char c);
bool setp(Node *p);
bool setn(Node *n);
static int allocation();
private:
int idata;
char cdata;
Node *prior;
Node *next;
static int count;
};
//node.cpp

include "node.h"

int Node::count=0;
int Node::readi() const
{
return idata;
}
char Node::readc() const
{
return cdata;
}
Node * Node::readp() const
{
return prior;
}
Node * Node::readn() const
{
return next;
}
bool Node::set(int i)
{
idata=i;
return true;
}
bool Node::set(char c)
{
cdata=c;
return true;
}
bool Node::setp(Node *p)
{
prior=p;
return true;
}
bool Node::setn(Node *n)
{
next=n;
return true;
}
Node::Node()
{
cout <<"Node constructor is running..." <<endl;
count++;
idata=0;
cdata='0';
prior=NULL;
next=NULL;
}
Node::Node(int i,char c)
{
cout <<"Node constructor is running..." <<endl;
count++;
idata=i;
cdata=c;
prior=NULL;
next=NULL;
}
Node::Node(int i,char c,Node *p,Node *n)
{
cout <<"Node constructor is running..." <<endl;
count++;
idata=i;
cdata=c;
prior=p;
next=n;
}
Node::Node(Node &n)
{
count++;
idata=n.idata;
cdata=n.cdata;
prior=n.prior;
next=n.next;
}
Node::~Node()
{
count--;
cout <<"Node destructor is running..." <<endl;
}
int Node::allocation()
{
return count;
}
//linklist.h

include "node.h"

include

using namespace std;
class Linklist
{
public:
Linklist(int i,char c);
Linklist(Linklist &l);
~Linklist();
bool Locate(int i);
bool Locate(char c);
bool Insert(int i=0,char c='0');
bool Delete();
void Show();
void Destroy();
static int Number();
private:
Node head;
Node * pcurrent;
static int count;
};
int Linklist::count=0;
Linklist::Linklist(int i,char c):head(i,c)
{
count++;
cout<<"Linklist constructor is running..."<<endl;
pcurrent=&head;
}
Linklist::Linklist(Linklist &l):head(l.head)
{
count++;
cout<<"Linklist Deep cloner running..." <<endl;
pcurrent=&head;
Node * ptemp1=l.head.readn();
while(ptemp1!=NULL)
{
Node * ptemp2=new Node(ptemp1->readi(),ptemp1->readc(),pcurrent,NULL);
pcurrent->setn(ptemp2);
pcurrent=pcurrent->readn();
ptemp1=ptemp1->readn();
}
}
Linklist::~Linklist()
{
count--;
cout<<"Linklist destructor is running..."<<endl;
Destroy();
}
bool Linklist::Locate(int i)
{
Node * ptemp=&head;
while(ptemp!=NULL)
{
if(ptemp->readi()==i)
{
pcurrent=ptemp;
return true;
}
ptemp=ptemp->readn();
}
return false;
}
bool Linklist::Locate(char c)
{
Node * ptemp=&head;
while(ptemp!=NULL)
{
if(ptemp->readc()==c)
{
pcurrent=ptemp;
return true;
}
ptemp=ptemp->readn();
}
return false;
}
bool Linklist::Insert(int i,char c)
{
if(pcurrent!=NULL)
{
Node * temp=new Node(i,c,pcurrent,pcurrent->readn());
if (pcurrent->readn()!=NULL)
{
pcurrent->readn()->setp(temp);
}
pcurrent->setn(temp);
return true;
}
else
{
return false;
}
}
bool Linklist::Delete()
{
if(pcurrent!=NULL && pcurrent!=&head)
{
Node * temp=pcurrent;
if (temp->readn()!=NULL)
{
temp->readn()->setp(pcurrent->readp());
}
temp->readp()->setn(pcurrent->readn());
pcurrent=temp->readp();
delete temp;
return true;
}
else
{
return false;
}
}
void Linklist::Show()
{
Node * ptemp=&head;
while (ptemp!=NULL)
{
cout <readi() <<'\t' <readc() <<endl;
ptemp=ptemp->readn();
}
}
void Linklist::Destroy()
{
Node * ptemp1=head.readn();
while (ptemp1!=NULL)
{
Node * ptemp2=ptemp1->readn();
delete ptemp1;
ptemp1=ptemp2;
}
head.setn(NULL);
}
int Linklist::Number()
{
return count;
}
//main.cpp

include "Linklist.h"

include

using namespace std;
int main()
{
int tempi;
char tempc;
cout <<"请输入一个整数和一个字符:" <<endl;
cin >>tempi >>tempc;
Linklist a(tempi,tempc);
a.Locate(tempi);
a.Insert(1,'C');
Linklist b(a);
cout <<"Number of Linklist is " <<Linklist::Number() <<endl;
return 0;
}
代码很长 其实就是创建链表的代码
想问下
Node::Node(Node &n)
{
count++;
idata=n.idata;
cdata=n.cdata;
prior=n.prior;
next=n.next;
}
有什么用 是不是跟拷贝构造函数有关?。。
求教 谢谢。。


你这个也不是标准的拷贝构造函数啊,当然但也是可以拷贝构造的。
标准的应该是常引用的赶脚:

Example(const Example& E);
【热门文章】
【热门文章】