⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_54122.htm

📁 vc
💻 HTM
字号:
<p>
序号:54122 发表者:牛屎一族 发表日期:2003-09-25 18:20:22
<br>主题:菜鸟关于拷贝构造函数问题求助!!!
<br>内容:.........<BR>class student<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;student(char*pname=&#34;no name&#34;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;constructing new student&#34;&lt;&lt;pname&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(name,pname,sizeof(name));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name[sizeof(name)-1]=&#34;'\0';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;student(student&amp;s)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;contructing copy of&#34;&lt;&lt;s.name&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(name,&#34;copy of&#34;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcat(name,s.name);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~student()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;destructing&#34;&lt;&lt;name&lt;&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;protected:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char name[40];<BR>};<BR><BR>class tutor<BR>{<BR>&nbsp;&nbsp;public:<BR>tutor(student&amp;s):student(s)//不明why 要加student(s)?<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;constructing tutor\n&#34;;<BR>}<BR><BR>&nbsp;&nbsp;protected:<BR>student student;//?????不是tutor student吗?<BR>};<BR>void fn(tutor tutor)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;in function fn()\n&#34;;<BR>}<BR>void main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;student randy(&#34;randy&#34;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;tutor tutor(randy);//怎样调用student的拷贝构造函数初始化tutor::student???<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;calling fn()\n&#34;;<BR>&nbsp;&nbsp;&nbsp;&nbsp;fn(tutor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;returned from fn()\n&#34;;<BR>}<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:凯凯 回复日期:2003-09-25 21:45:27
<br>内容:tutor(student&amp;s):student(s)//不明why 要加student(s)?<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;constructing tutor\n&#34;;<BR>}<BR><BR><BR>protected:<BR>student student;//?????不是tutor student吗?<BR><BR>后两句申明了一个student类的变量也叫student(这是不良的编程习惯)<BR>最前面的一句用输入参数s来初始化student变量。<BR>之所以放到那个地方而不放到函数体中,是因为这样可以省去一次缺省构造。<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Bardo 回复日期:2003-09-25 21:56:19
<br>内容:.........<BR>class student<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;student(char*pname=&#34;no name&#34;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;constructing new student&#34;&lt;&lt;pname&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(name,pname,sizeof(name));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name[sizeof(name)-1]=&#34;'\0';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;student(student&amp;s)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;contructing copy of&#34;&lt;&lt;s.name&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(name,&#34;copy of&#34;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcat(name,s.name);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~student()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;destructing&#34;&lt;&lt;name&lt;&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;protected:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char name[40];<BR>};<BR><BR>class tutor<BR>{<BR>&nbsp;&nbsp;public:<BR>tutor(student&amp;s):student(s)//不明why 要加student(s)?<BR>//这就象派生类一样,是调用类student的构造函数来初始化当前类中的对象成员student,当前类获取参数供后面调用时使用。所以,前面是构造定义,&#34;:&#34;后面是当前类的student类型的成员的构造函数的调用。<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;constructing tutor\n&#34;;<BR>}<BR><BR>&nbsp;&nbsp;protected:<BR><BR>student student;//?????不是tutor student吗?<BR>//不是,这是一个类成员,前一个student是数据类型,后一个是对象名。这个数据面员是一个对象,所以上面要用拷贝构造函数调用student类的构造函数初始化。<BR>};<BR>void fn(tutor tutor)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;in function fn()\n&#34;;<BR>}<BR>void main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;//先构造一个student对象<BR>&nbsp;&nbsp;&nbsp;&nbsp;student randy(&#34;randy&#34;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;tutor tutor(randy);//怎样调用student的拷贝构造函数初始化tutor::student???<BR>&nbsp;&nbsp; //这里是将已定义好的对象拷贝到tutor对象中。所以,此类函数才称为拷贝构造函数。拷贝构造函数类中已定义好了。所以,它会象普通代码一样被执行。所以,这里只要构造tutor对象,student(s)就会被调用,而参数就是当前传入的randy,你看看,这是不是COPY?<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;calling fn()\n&#34;;<BR>&nbsp;&nbsp;&nbsp;&nbsp;fn(tutor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&#34;returned from fn()\n&#34;;<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:牛屎一族 回复日期:2003-09-25 22:55:21
<br>内容:干麻偏要s初始化,然后调用:student构造函数student(s)
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:牛屎一族 回复日期:2003-09-25 23:24:17
<br>内容:调用:student构造函数student(s),不是要执行1次<BR>cout&lt;&lt;&#34;constructing new student&#34;&lt;&lt;pname&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(name,pname,sizeof(name));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name[sizeof(name)-1]=&#34;'\0';?????<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:困惑的人 回复日期:2003-09-26 09:54:48
<br>内容:不是偏要s来初始化,tutor(student&amp;s):student(s)这句话的意思我认为是tutor函数的成员是类student中的一个对象,当然你明白student中的对象就要调用student中的构造函数了。<BR>s只是指明是student中的对象,你可以换成别的字母,如b,c之类的。不要在这个上面想的太多,明白这里的意思就可以了。<BR>name[sizeof(name)-1]='\0';不明白吗?<BR>你知道数组中最后一个字符是以0吧。不用说了吧。<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:牛屎一族 回复日期:2003-09-26 12:50:24
<br>内容:是不是说当student(student&amp;s)中的student&amp;s被定义了, 所以student(student&amp;s)就会被执行,然后把student(student&amp;s)当作tutor tutor(randy)的构造函数
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:困惑的人 回复日期:2003-09-26 13:55:42
<br>内容:student&amp;s 是为类定义个对象了,你说类的构造函数student(student&amp;s)要不要执行呢?嘿嘿。<BR>是将上面定义好的对象作为tutor的对象。此时这个“randy&#34;已经被student类初始化了,也就是说student的构造函数把&#34;randy&#34;作为参数调用了,也就是所谓的拷贝构造函数。<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Bardo 回复日期:2003-09-26 14:05:58
<br>内容:构造函数即:优先初始化! 这是一个根本概念。<BR>但是,基类要在派生类初始化前先初始化<BR>但是,成员类要在主类初始化前先初始化<BR><BR>通过:类在构造时就要调用这些类的构造函数。用这一方法实现这一目的。<BR><BR>基类优先。因为,有其父才有其子。<BR>成员类其次。因为,成员类不构造成对象,就象一个人,五脏还没有生成。<BR>而且,还不好通过代码判断,如果成员类是私有对象,那就绝无可能测出是否已初始化!<BR>自然,在主类构造时必须先基类,然后成员类。因而,只有通过构造函数调用才能在主类构造函数执行前执行。<BR><BR>这就是上面代码要这么做的目的。<BR><BR>(我很笨,而且专门骗人!所以,仅供参考!不要认真!实际我什么都不懂!)
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -