注册。复制构造函数 [英] Reg. copy constructor

查看:59
本文介绍了注册。复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

能否请您解释一下复制构造函数的用法以及我们如何使用它。

给我一些例子。

问候,

Sudha。

Hi all,
Can you please explain me the usage of copy constructor and how do we use it.
Give me some example.
Regards,
Sudha.

推荐答案

su ********** @ yahoo.com (csudha)写道:
su**********@yahoo.com (csudha) writes:
你能解释一下我吗?复制构造函数的用法以及我们如何使用它。
Can you please explain me the usage of copy constructor and how do we use it.




请在comp.lang.c ++中询问C ++问题。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);} return 0;}



Please ask C++ questions in comp.lang.c++.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


csudha写道:
大家好,
你能解释一下c的用法吗? opy构造函数以及我们如何使用它。
给我一些例子。
Hi all,
Can you please explain me the usage of copy constructor and how do we use it.
Give me some example.




这是comp.lang.c,你可能正在寻找comp.lang .c ++

在C中,有(先验的)没有构造函数。

在询问comp.lang.c ++之前,先看看他们的常见问题解答

提问。

干杯

Michael

-

电子邮件:我的是an / at / gmx / dot / de address。



This is comp.lang.c, you are probably looking for comp.lang.c++
In C, there are (a priori) no constructors.
Before asking in comp.lang.c++, have a look at their FAQ before
asking the question.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


csudha写道:

大家好,


您好。我从其他对你文章的回答中注意到,有些人对于时事性有非常有趣的想法。好吧,不能帮助。

能否请您解释一下复制构造函数的用法以及我们如何使用它。


当然。很高兴有责任。


当我们希望创建一个新对象时,我们使用复制构造函数

是现有对象的精确副本。这在

情况下特别有用,我们不希望公开

对象的内部数据(数据隐藏)或我们的对象包含的内容指向

动态数据(这样只会复制对象表示对象的
会产生悬挂指针的危险当我们

正在清理)。

给我一些例子。

Hi all,
Hello. I note from the other responses to your article that some people
have very funny ideas about topicality. Oh well, can''t be helped.
Can you please explain me the usage of copy constructor and how do we use it.
Of course. Glad to oblige.

We use a copy constructor when we wish to create a new object that is
an exact copy of an existing object. This is particularly useful in
situations where we don''t wish to expose the inner data of the
object ("data hiding"), or where our object contains pointers to
dynamic data (so that merely duplicating the object representation
of the object would create a danger of a "dangling pointer" when we
are cleaning up).
Give me some example.




当然。以下示例已经在

bcc32(Borland C ++ 5.3,在主题模式下调用)和gcc(在主题模式下再次

)下编译和测试,并且未在下面进行诊断编译器

(在gcc下,使用标志-W -Wall -ansi -pedantic -O2)。


程序的输出可以是在本回复结尾处找到。


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


struct T_

{

int this;

int that;

char * theother;

};


/ *深拷贝的辅助例程* /

char * sduplicate(const char * s)

{

char * new = NULL;

if(s!= NULL)

{

new = malloc(strlen(s)+ 1);

if(new!= NULL )

{

strcpy(new,s);

}

}

返回新的;

}


typedef struct T_ T;


/ *首先,析构函数* /

void TDestroy(T ** old)

{

if(old!= NULL)

{

if(* old!= NULL)

{

free((* old) - > theother);

免费(*旧);

* old = NULL;

}

}

}


/ *现在,一个普通的构造函数* /

T * TCreate(int this,int that,const char * theother)

{

T * new = malloc(sizeof * new);

if(new!= NULL)

{

new-> this = this;

new-> that = that;

new-> theother = sduplicate(theother);

if(new-> theother == N. ULL)

{

/ *如果我们不能正确构建它,

根本不构建它! * /

TDestroy(& new);

}

}

返回新的;

}


/ *现在我们可以编写一个漂亮的简单复制构造函数* /

T * TCreateCopy(const T * old)

{

返回TCreate(old-> this,old-> that,old-> theother);

}


int TModifyString(T * p,const char * new)

{

int rv = 1; / *错误* /

if(p!= NULL&& new!= NULL)

{

char * q = sduplicate (新);

if(q!= NULL)

{

free(p-> theother);

p-> theother = q;

rv = 0; / *成功* /

}

}


返回rv;

}


/ *购买凭证* /

int TPrint(FILE * fp,T * p)

{

int rv = 0;

if(fp!= NULL&& p!= NULL)

{

rv = fprintf (fp,

"%d%d%s \ n",

p-> this,

p->那个,

p->另一个);

}

返回rv;

}


/ *测试驱动程序* /

int main(无效)

{

T * p = TCreate( 123,456,hello,world);

if(p!= NULL)

{

T * q = TCreateCopy( p);

if(q!= NULL)

{

fputs(" p =",stdout);

TPrint(stdout,p);

fputs(" q =",stdout);

TPrint(stdout,q); / *显示副本工作* /

put(现在修改p);

if(TModifyString(p,goodbye,world)== 0)

{

fputs(" p =",stdout);

TPrint(stdout,p);

fputs(" q =",stdout);

TPrint(stdout,q); / *表明q不受变化的影响

到p,即深拷贝。已完成* /

}

TDestroy(& q);

}

TDestroy(& p );

}

返回0;

}


此程序的输出是:


p = 123 456你好,世界

q = 123 456你好,世界

现在修改p

p = 123 456再见,世界

q = 123 456你好,世界

HTH。手。



Sure. The following example has been compiled and tested under
bcc32 (Borland C++ 5.3, invoked in topical mode) and gcc (again
in topical mode), and gave no diagnostics under either compiler
(under gcc, the flags -W -Wall -ansi -pedantic -O2 were used).

The program''s output can be found at the end of this reply.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct T_
{
int this;
int that;
char *theother;
};

/* helper routine for a "deep copy" */
char *sduplicate(const char *s)
{
char *new = NULL;
if(s != NULL)
{
new = malloc(strlen(s) + 1);
if(new != NULL)
{
strcpy(new, s);
}
}

return new;
}

typedef struct T_ T;

/* first, a destructor */
void TDestroy(T **old)
{
if(old != NULL)
{
if(*old != NULL)
{
free((*old)->theother);
free(*old);
*old = NULL;
}
}
}

/* now, an ordinary constructor */
T *TCreate(int this, int that, const char * theother)
{
T *new = malloc(sizeof *new);
if(new != NULL)
{
new->this = this;
new->that = that;
new->theother = sduplicate(theother);
if(new->theother == NULL)
{
/* if we can''t build it properly,
don''t build it at all! */
TDestroy(&new);
}
}
return new;
}

/* now we can code a nice simple copy constructor */
T *TCreateCopy(const T* old)
{
return TCreate(old->this, old->that, old->theother);
}

int TModifyString(T *p, const char *new)
{
int rv = 1; /* error */
if(p != NULL && new != NULL)
{
char *q = sduplicate(new);
if(q != NULL)
{
free(p->theother);
p->theother = q;
rv = 0; /* success */
}
}

return rv;
}

/* proof of purchase */
int TPrint(FILE *fp, T *p)
{
int rv = 0;
if(fp != NULL && p != NULL)
{
rv = fprintf(fp,
"%d %d %s\n",
p->this,
p->that,
p->theother);
}
return rv;
}

/* test driver */
int main(void)
{
T *p = TCreate(123, 456, "hello, world");
if(p != NULL)
{
T *q = TCreateCopy(p);
if(q != NULL)
{
fputs("p = ", stdout);
TPrint(stdout, p);
fputs("q = ", stdout);
TPrint(stdout, q); /* show that the copy worked */
puts("Modifying p now");
if(TModifyString(p, "goodbye, world") == 0)
{
fputs("p = ", stdout);
TPrint(stdout, p);
fputs("q = ", stdout);
TPrint(stdout, q); /* show that q is unaffected by the change
to p, ie a "deep copy" was done */
}
TDestroy(&q);
}
TDestroy(&p);
}
return 0;
}

The output from this program is:

p = 123 456 hello, world
q = 123 456 hello, world
Modifying p now
p = 123 456 goodbye, world
q = 123 456 hello, world
HTH. HAND.


这篇关于注册。复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆