ÈçºÎʹÓ÷ºÐÍÄ£擬virtual? (C/C++) (template)
ÍøÉÏÒ»µÀ½ðɽµÄÃæÊÔÌ⣺
http://topic.csdn.net/u/20100524/14/0eff992a-2849-4db6-bdaa-d4a200e79b7c.html
Çë·Ö±ðÓÃC++µÄÃæÏò¶ÔÏóºÍ·ºÐÍ»úÖÆ£¬±àдʵÏÖTemplate MethodģʽµÄʾÀý´úÂ룬²¢±È½ÏÁ½ÖÖ·½Ê½¸÷×ÔµÄÓÅȱµã¡£
ÓÃÐ麯ÊýʵÏÖTemplate MethodµÄ·½Ê½¾Í²»¶à˵ÁË¡£Ó÷ºÐ͵ķ½Ê½ÊµÏÖ¶à̬ÔÚATLÀïÃæÓдóÁ¿µÄÓõ½£¡
·ºÐ͵ÄÓÅÊƾÍÔÚÓÚÆäÖ´ÐÐЧÂʸߣ¡±àÒëÆÚ¶à̬£¡ÕâÒ²ÊÇATL/WTLÓÅÓÚMFCµÄµØ·½£¡
ÖÁÓÚ·ºÐÍʵÏÖ¶à̬µÄÁÓÊÆ£¬ÎÒÍüÁË£¬µ«Ó¦¸ÃÊÇ·ºÐÍûÓÐÐ麯Êý¹¦ÄÜÇ¿´ó£¬ºÃÏñÔڶ༶¼Ì³ÐϾÍʧЧÁË°É£¿±ÈÈçÖ»ÄÜÓÃÓÚBASE-DeriveÁ½¼¶¼Ì³Ð£¬¶ÔÓÚBase-Derive-DeriveDeriveÔò²»ÐУ¡
²»¶à˵ÁË£¬¿´¿´ÈçºÎÓ÷ºÐÍʵÏÖTemplate Methodģʽ°É¡££º£©
ת×Ô£ºhttp://kb.cnblogs.com/a/873808/
Abstract
OO×îÖØÒªµÄ¶àÐÍ/¶à態¾ÍÊÇÓÃ繼³Ð + virtual來Íê³É£¬virtual雖ºÃÓ㬵«¸¶³öµÄ´ú價¾ÍÊÇ¡º¸ü¶àµÄ記憶體¡»+¡º¸üÂýµÄËٶȡ»£¬±¾ÎĽé紹ʹÓ÷ºÐÍ來Ä£擬virtual機ÖÆ¡£
Introduction
Ê×ÏÈ聲Ã÷£¬這並²»ÊÇÎÒ獨創µÄ·½·¨£¬¶øÊÇATLËùʹÓõķ½Ê½£¬請參¿¼(轉貼) ATL²¼á£Ö®ÏµÄÃØÃÜ(3) (C++) (template)
為ʲ麼ÒªÓ÷ºÐÍ來Ä£擬virtual機ÖÆÄØ?
Ö÷ÒªÊÇÒò為virtual機ÖÆÐèÀûÓõ½vtable£¬ËùÒÔ較Õ¼記憶體£¬Óֵÿ¿vptrÔÚrun-time執ÐжàÐÍ£¬ËùÒÔ執ÐÐËÙ¶È較Âý£¬ËùÒÔATL²ÅÏëÀûÓ÷ºÐÍ這種compile-time機ÖÆ讓component盡¿ÉÄܵÄС£¬盡¿ÉÄܵĿ졣
Example Code
/**//*
(C) OOMusou 2007 http://oomusou.cnblogs.com
Filename : template_virtual.cpp
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
Description : Demo how to use generics to simulate virtual
Release : 08/28/2007 1.0
*/
#include <iostream>
using namespace std;
template <typename Derived>
class Base {
public:
void func()&nb
Ïà¹ØÎĵµ£º
CµÄstaticµÄÓ÷¨:
1.¶ÔÓÚº¯ÊýÀïµÄ¾Ö²¿±äÁ¿,¸Ä±äµÄÊÇËüµÄÉú´æÖÜÆÚ,Õâ¸ö±äÁ¿»áÒ»Ö±´æÔÚ,µ½³ÌÐò½áÊø. º¯ÊýÍⲿ·ÃÎʲ»µ½Õâ¸ö±äÁ¿.
2.¶ÔÓÚÈ«¾ÖµÄ±äÁ¿,¸Ä±äµÄÊÇËüµÄ×÷Ó÷¶Î§,Õâ¸ö±äÁ¿Ö»ÔÚ±¾ÎļþÄÚÓÐЧ.ÆäËüµÄ.cÎļþ¿´²»µ½.
µ±È»ËüµÄÉúÃüÖÜÆÚÊǺͳÌÐòÒ»ÑùµÄ ......
¿ìËÙÃÝÈ¡Ä£ÀíÂÛ»ù´¡£º ¼ÆËã a^b mod c ?
ÓÉ(a x b) mod c=((a mod c) x b) mod c.
ÎÒÃÇ¿ÉÒÔ½« bÏȱíʾ³É¾Í£º
b=at2^t+at-1 2^t-1+……a02^0. (ai=[0,1]).
ÕâÑùÎÒÃÇÓÉ a^b mod c=(a^(at2^t+at-12^t-1+…a02^0)mod c.
È»¶øÎÒÃÇÇó a^(2^(i+1)) ......
×Ö·û´®ÊÇCÓïÑԵľ«»ªÖ®Ò»£¬CÓïÑÔÊDZÊÊÔ¿¼ÌâµÄºËÐÄ֪ʶµã£¬²»¹ÜÄãÊÇѧc++£¬Ñ§java»¹ÊÇc#¡¢.net£¬»¹Êǽű¾Ê²Ã´µÄ£¬×îºÃ»¹ÊǶ®µãC°É£¬²»¶®C±ÊÊÔ»òÐí±È½Ï³Ô¿÷¡£ËùÒÔÔÚ±ÊÊÔÃæÊÔÖУ¬×Ö·û´®µÄÎÊÌâÊǾ³£³öÏֵģ¬ÆäÖÐÓÐÒ»ÀàÎÊÌâ¾ÍÊÇ×Ô¼ºÊµÏÖһЩ»ù±¾µÄC±ê×¼¿âµÄ×Ö·û´®´¦Àíº¯Êý»òÆäËûC±ê×¼¿âº¯Êý¡£
ÎÊÌâÒ»ÃèÊö£º Óñê×¼Cдһ¸öÅжϸ ......
·ÇʱÉСñ2010ÐÂ¿î ¹¬Í¢¸´¹ÅÂÞÂíЬÕæƤÁ¹Ð¬ÊæÊÊƽµ×ЬŮЬ
189.0Ôª
Ò»¸öÓÉC/C++±àÒëµÄ³ÌÐòÕ¼ÓõÄÄÚ´æ·ÖΪÒÔϼ¸¸ö²¿·Ö
1¡¢Õ»Çø£¨stack£©— ³ÌÐòÔËÐÐʱÓɱàÒëÆ÷×Ô¶¯·ÖÅ䣬´æ·Åº¯ÊýµÄ²ÎÊýÖµ£¬¾Ö²¿±äÁ¿µÄÖµµÈ¡£Æä²Ù×÷·½Ê½ÀàËÆÓÚÊý¾Ý½á¹¹ÖеÄÕ»¡£³ÌÐò½áÊøʱÓɱàÒëÆ÷×Ô¶¯ÊÍ·Å¡£
2¡¢¶ÑÇø£¨h ......