[C/C++ Digestion] – Rule of Three, ¸´ÖÆ¿ØÖÆ
[C/C++ Digestion] – Rule of Three,
¸´ÖÆ¿ØÖÆ
×÷Õߣº
Jason Lee @
http://blog.csdn.net/jasonblog
ÈÕÆÚ£º
2010-04-13
[1]
¸´Öƹ¹Ô캯Êý
copy constructor
Rule of Three
ÊÇÖ¸ÀàÈç¹ûÐèÒªÎö¹¹º¯Êý£¬Ôòͨ³£Ò²ÐèÒª¸´Öƹ¹Ô캯ÊýºÍ¸³Öµ²Ù×÷·û¡£¶øÆäʵϰ¹ßµØÏÔʾ±àдÕâÈýÕß±¾¾ÍÊÇÒ»¸öÁ¼ºÃµÄÏ°¹ß¡£ÒòΪÏà½ÏÓÚ±àÒëÆ÷×Ô¶¯Éú³ÉµÄ´úÂ룬ÏÔʾ±àд×Ô¼ºµÄ´úÂëÄÜÈóÌÐòÔ±¶ÔÕû¸ö³ÌÐòÓÐןüÇåÎúµÄÈÏʶºÍ°ÑÎÕ¡£
´ÓÐÎʽÉÏ˵£¬¸´Öƹ¹Ô캯Êý¾ßÓе¥¸ö±¾ÀàÐͶÔÏóÒýÓõÄÐβΣ¨Í¨³£ÓÃ
const
ÏÞ¶¨ÐÞÊΣ©£¬²¢ÇÒº¯ÊýÃûÓëÀàÃûÏàͬ£¬ÒòΪ¸´Öƹ¹Ô캯ÊýÒ²Êǹ¹Ô캯Êý¡£±ÈÈ磺
Demo(const Demo &demo){}
¸´Öƹ¹Ô캯Êý´ÓÐèÒªÉÏÀ´¿´ÓÐÁ½ÖÖÇé¿ö£¬Ò»ÊÇÓгÉÔ±ÔÚ¹¹Ô캯ÊýÖзÖÅä×ÊÔ´£¬±ÈÈçÖ¸Õë
ÊÇ×îµäÐ͵ÄÀý×Ó£»¶þÊÇÐèÒªÔÚ¸´Öƹý³Ì×öÌض¨¹¤×÷¡£
´Ó×÷Óó¡ºÏÀ´Ì¸£¬±¾ÖÊÉÏ¿ÉÒÔ¿´×öÊÇÖÁÉÙ´æÔÚÁ½¸ö¶ÔÏóµÄÇé¿öÏÂÐèÒª¸´Öƹ¹Ô캯Êý£¬ÒòΪÐèҪʹÓÃÆäÒ»À´¶ÔÆäËü¶ÔÏó½øÐгõʼ»¯¡£±ÈÈ磺
1.
¸ù¾ÝͬÀàÐͶÔÏó³õʼ»¯Ä³Ò»¶ÔÏó£»ÎÒ¸öÈ˾õµÃÔÚÕâÖÖÇé¿öÏ£¬ºÜÈÝÒ×ÊèºöµÄÒ»¸öϸ½ÚÖ®´¦ÊÇ£ºÒ»°ã£¬¶Ô¹¹Ô캯Êý±È½ÏÉúÊèµÄ³ÌÐòԱϰ¹ßµØ¶¨ÒåÒ»¸öĬÈϹ¹Ô캯Êýºó£¬²¢ÇÒÔÚÀïÃæ½øÐÐÖ¸ÕëËùÐè¿Õ¼äµÄ¿ª±ÙºÍ·ÖÅ䣬¾Í»áDZÒâʶµØÒÔΪÒÔºóÿ¸ö¸ÃÀà¶ÔÏó³õʼ»¯Ê±¶¼»áºÏÀíµØÊ×ÏȶÔÖ¸Õë³ÉÔ±½øÐÐÄÚ´æ·ÖÅ䣬Ȼ¶øȴûÓÐ×ã¹»Éî¿ÌµØÒâʶµ½Èç¹ûµ÷ÓÃÁ˸´Öƹ¹Ô캯Êý£¬Ôò²»»áµ÷ÓÃĬÈϺ¯Êý£º
#include <iostream>
using namespace std;
class Demo {
public:
Demo(){ p = new int(1); }
Demo(const Demo &demo){ *p = *demo.p; }//Ö¸Õëp²¢Î´±»·ÖÅä¿Õ¼ä
~Demo(){}
void setP(int v){ *p = v; }
void show(){ cout << *p << endl; }
private:
int *p;
};
int main(){//³ÌÐòÔËÐÐʱ³ö´í
Demo d1;
d1.setP(3);
Demo d2 = d1;//Ö»µ÷Óø´Öƹ¹Ô캯Êý£¬²¢Ã»Óе÷ÓÃĬÈϹ¹Ô캯Êý£¬ËùÒÔÖ¸ÕëδӵÓпռä
return 0;
}
2.
½«¶ÔÏó×÷ΪÆÕͨʵ²Î£»ÕâÖÖÇé¿ö×îÈÝÒ×ÓëÖµ´«µÝµÄÇé¿ö½øÐÐÁªÏ룺´«µÝµÄÊÇÓë±äÁ¿µÈÖµµÄÒ»¸öÁÙʱֵ£¬¶ø·Çͬһ¸ö±äÁ¿¡£Í¬ÑùµÄ£¬ÒÔ¶ÔÏó×÷ΪÆÕͨʵ²Î£¬Ò²ÐèÒª½¨Á¢Ò»¸öÁÙʱ¶ÔÏ󣬾ÍÐèÒªµ÷Óõ½¸´Öƹ¹Ô캯Êý¡£±ÈÈçÒÔÏÂÒ»¶ÎºÁÎÞÒâÒåµÄ´úÂ룺
3.
´Óº¯ÊýÖзµ»ØÒ»¸öÆÕͨ¶ÔÏó£»ÕâÖÖÇé¿öµÄµÀÀíÓëÉÏÒ»ÖÖÇé¿öÒ»Ñù£¬Á½ÖÖÇé¿ö¿ÉÒÔ³öÏÖÔÚÒ»¶Î´úÂëÖУº
Demo Test(Demo demo){
Demo temp;
return temp;
}
4.
Ïȵ÷Óù¹Ô캯Êý´´½¨Ò»¸öÁÙʱ¶ÔÏó£¬ÔÙµ÷Óø
Ïà¹ØÎĵµ£º
תÌû×Ôhttp://teren.itpub.net/post/15914/189516дµÄ·Ç³£ºÃ£¬Í¬Ê±¶Ô×÷Õß´òÆÆÉ°¹øÎʵ½µ×µÄ¾«Éñ³¹µ×ÕÛ·þ
¡¶³¹µ×¸ã¶¨CÖ¸Õë¡·µÚһƪ ±äÁ¿µÄÄÚ´æʵÖÊ
ÒªÀí½âCÖ¸Õ룬ÎÒÈÏΪһ¶¨ÒªÀí½âCÖГ±äÁ¿”µÄ´æ´¢ÊµÖÊ£¬ËùÒÔÎҾʹӓ±äÁ¿”Õâ¸ö¶«Î÷¿ªÊ¼½²Æð°É£¡
ÏÈÀ´Àí½âÀí½âÄÚ´æ¿Õ¼ä°É£¡Çë¿´ÏÂͼ£º
ÄÚ´æµØ ......
C++µÄʵÏÖ
#include<fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream logTest("foo.log");
streambuf *oldbuf = cout.rdbuf(logTest.rdbuf());
cout << "Êä³ö ......
//Á¬½ÓÍ·Îļþ
#include <io.h>
#include <dir.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//¿½±´ÎļþÄ£¿é
int copyfile (char *infile, char *outfile)
{
FILE *in,*out; //¶¨ÒåÎļþÖ¸Õë
in = fopen(infile,"r"); //´ ......
µÚһƪ£ºtypedef structÓëstructµÄÇø±ð
1. »ù±¾½âÊÍ
typedefΪCÓïÑԵĹؼü×Ö£¬×÷ÓÃÊÇΪһÖÖÊý¾ÝÀàÐͶ¨ÒåÒ»¸öÐÂÃû×Ö¡£ÕâÀïµÄÊý¾ÝÀàÐÍ°üÀ¨ÄÚ²¿Êý¾ÝÀàÐÍ£¨int,charµÈ£©ºÍ×Ô¶¨ÒåµÄÊý¾ÝÀàÐÍ
£¨structµÈ£©¡£
ÔÚ±à³ÌÖÐʹÓÃtypedefÄ¿µÄÒ»°ãÓÐÁ½¸ö£¬Ò»¸öÊǸø±äÁ¿Ò»¸öÒ×¼ÇÇÒÒâÒåÃ÷È·µÄÐÂÃû×Ö£¬ÁíÒ»¸öÊǼò»¯Ò»Ð©±È½Ï¸´ÔÓµÄÀàÐÍÉùÃ÷ ......
ÍøÉÏÓÐÁ˺ܶàÕâÑùµÄÎÄÕ£¬²»¹ý¸Ð¾õ²¢²»ÊǺÜÈ«£¬ÎÒ¶¼¶«ÕÒÎ÷ÕÒ²Å×îÖÕ°ÑJAVAµ÷ÓÃc++¸ø¸ã¶¨¡£
ÏÂÃæÒýÓÃÁ˺ܶàÍøÉÏÒѾÓеĵÄÄÚÈÝ£¬ÎÒÔÚÕâÀïÖ»ÊÇÕûºÏÒ»´Î£¬ÈÃÒÔºóµÄÅóÓÑÉÙ×ßÍø·¡£
¶à˵ÎÞÒ棬¾ßÌåÈçÏ£º
×÷Õß:mykoma
¼ÙÉèËùÓÐÎļþ¶¼·ÅÔÚd:\
Step 1:
дJAVAÎļþ
ÀýÈç³ÌÐòtestdll.java£¬ÄÚÈÝΪ£º
......