??xml version="1.0" encoding="utf-8" standalone="yes"?>国产午夜免费高清久久影院,国产人久久人人人人爽,77777亚洲午夜久久多喷http://www.shnenglu.com/jacky2019/category/3690.htmlq个kisser不太?/description>zh-cnThu, 01 Apr 2010 17:08:38 GMTThu, 01 Apr 2010 17:08:38 GMT60c++ volatile关键?/title><link>http://www.shnenglu.com/jacky2019/archive/2010/04/01/111249.html</link><dc:creator>kk</dc:creator><author>kk</author><pubDate>Thu, 01 Apr 2010 02:54:00 GMT</pubDate><guid>http://www.shnenglu.com/jacky2019/archive/2010/04/01/111249.html</guid><wfw:comment>http://www.shnenglu.com/jacky2019/comments/111249.html</wfw:comment><comments>http://www.shnenglu.com/jacky2019/archive/2010/04/01/111249.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.shnenglu.com/jacky2019/comments/commentRss/111249.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/jacky2019/services/trackbacks/111249.html</trackback:ping><description><![CDATA[<span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Simsun; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span>c++中的volatile关键字一直没有用过。最q用了一下。该关键字的意思就是告诉编译器不要对该变量q行优化Q用地址对该变量q行取D不要直接用该变量已经存在cach/寄存器中的倹{?br><br>试代码如下Q?br> <div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080"> 1</span><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> volatile.cpp : Defines the entry point for the console application.<br></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #008000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//<br></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #008000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">stdafx.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> _tmain(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, _TCHAR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> argv[])<br></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #000000"><img id=Codehighlighter1_134_292_Open_Image onclick="this.style.display='none'; Codehighlighter1_134_292_Open_Text.style.display='none'; Codehighlighter1_134_292_Closed_Image.style.display='inline'; Codehighlighter1_134_292_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_134_292_Closed_Image onclick="this.style.display='none'; Codehighlighter1_134_292_Closed_Text.style.display='none'; Codehighlighter1_134_292_Open_Image.style.display='inline'; Codehighlighter1_134_292_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_134_292_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_134_292_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">volatile int a = 1;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #008000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> a </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    printf (</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a = %d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, a);<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    __asm<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_206_238_Open_Image onclick="this.style.display='none'; Codehighlighter1_206_238_Open_Text.style.display='none'; Codehighlighter1_206_238_Closed_Image.style.display='inline'; Codehighlighter1_206_238_Closed_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_206_238_Closed_Image onclick="this.style.display='none'; Codehighlighter1_206_238_Closed_Text.style.display='none'; Codehighlighter1_206_238_Open_Image.style.display='inline'; Codehighlighter1_206_238_Open_Text.style.display='inline';" align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif">    </span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_206_238_Closed_Text><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_206_238_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">        mov DWORD ptr [ebp</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">], 10h // 修改a的gؓ0x10<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">    }</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    printf (</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a = %d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, a);<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    getchar();<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span></div> </span><br>如果~译器没有优化的话,打印出来的值应该是1?6。反之,如果没有使用volatileQ优化之后应该打印出来的??<br><br>我在vc6和vc2008下测试了一下:<br>-----------------------------------------------------------------------------------------<br><br>vc2008<br>-----------------------------------------------------------------------------------------<br>没有关键字volatile<br>debug下打印的?1 ?1 ---Z么会被优化了呢?我的优化~译选项是disabled?br>release下打印的?1 ?1<br>有关键字volatile<br><font face=宋体>debug下打印的?1 ?1 --- 很奇怪!<br>release下打印的?1 ?16<br><br>我想可能是编译器对debug下作了特D处理。而且该程序非常简?/font><br><br>vc6.0<br><font face=宋体>-----------------------------------------------------------------------------------------<br>没有关键字volatile<br><font face=宋体>debug下打印的?1 ?16<br>release下打印的?1 ?1</font><br>有关键字volatile<br><font face=宋体>debug下打印的?1 ?16<br>release下打印的?1 ?1 --- 很奇怪!<br><br><br>结Q?br>在多U程多核的情况下Q要防止被编译器优化Q?/font></font> <img src ="http://www.shnenglu.com/jacky2019/aggbug/111249.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/jacky2019/" target="_blank">kk</a> 2010-04-01 10:54 <a href="http://www.shnenglu.com/jacky2019/archive/2010/04/01/111249.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>环Ş~冲http://www.shnenglu.com/jacky2019/archive/2010/04/01/111243.htmlkkkkThu, 01 Apr 2010 01:17:00 GMThttp://www.shnenglu.com/jacky2019/archive/2010/04/01/111243.htmlhttp://www.shnenglu.com/jacky2019/comments/111243.htmlhttp://www.shnenglu.com/jacky2019/archive/2010/04/01/111243.html#Feedback0http://www.shnenglu.com/jacky2019/comments/commentRss/111243.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/111243.html环Ş~冲是可以不用加锁的。下面是一个实现的环Ş~冲。用c实现Q可能会出现write满情况,q种情况D的结果就是write不成功,数据没写q去?/pre>
我这里只是简单的丢了?/pre>
  1 //
  2 
  3 #include "stdafx.h"
  4 #include <windows.h>
  5 #include <time.h>
  6 #include <stdlib.h>
  7 
  8 #define MAX_BUF_LEN 20        // buf len // 必须要大?
  9 int nRead = 0;                // read pos
 10 int nWrite = 0;                // write pos
 11 int buf[MAX_BUF_LEN] = {0}; // loop buf
 12 
 13 #define NN 20000
 14 __int64 sum = 0;
 15 __int64 lose = 0;
 16 __int64 total = (__int64)(1+NN)*(__int64)NN/2;
 17 bool bWriteFinished = false;
 18 
 19 void WriteBuf(int n)
 20 {
 21     int rpos = nRead;
 22     int wpos = nWrite;
 23     wpos++;
 24     // buf满(即write又追上了readQ? 注ؓ了避免与初始状态重复判?br> 25     // 所以这里最后一个buf没有写就认ؓ已经满了。所以MAX_BUF_LEN不能?
 26     // q里Q直接丢弃该buf
 27     if (wpos % MAX_BUF_LEN == rpos % MAX_BUF_LEN)
 28     {
 29         lose += n;
 30         return;
 31     }
 32 
 33     buf[nWrite] = n;
 34     nWrite++;
 35     if (nWrite == MAX_BUF_LEN)
 36         nWrite = 0;
 37 }
 38 
 39 int ReadBuf()
 40 {
 41     if (nWrite == nRead)
 42         return -1;
 43 
 44     int n = buf[nRead];
 45     nRead++;
 46     if (nRead == MAX_BUF_LEN)
 47         nRead = 0;
 48     return n;
 49 }
 50 
 51 DWORD WINAPI ReadThread(LPVOID lpParameter)
 52 {
 53     int tmp = 0;
 54     int count = 0;
 55     while (true)
 56     {
 57         //printf("ReadThread = %d\n", ReadBuf());
 58         tmp = ReadBuf();
 59         if (-1 != tmp)
 60         {
 61             count++;
 62             sum += tmp;
 63             if (count == NN)
 64             {
 65                 printf ("ReadThread finished!\n");
 66                 break;
 67             }
 68         }
 69         else if (bWriteFinished)
 70         {
 71             printf ("ReadThread finished222!\n");
 72             break;
 73         }
 74 
 75         Sleep(rand() % 10);
 76     }
 77     return 0;
 78 }
 79 
 80 DWORD WINAPI WriteThread(LPVOID lpParameter)
 81 {
 82     int n = 0;
 83     while (true)
 84     {
 85         n++;
 86         WriteBuf(n);
 87         if (n == NN)
 88         {
 89             printf ("WriteThread finished!\n");
 90             bWriteFinished = true;
 91             break;
 92         }
 93 
 94         Sleep(rand() % 10);
 95     }
 96     return 0;
 97 }
 98 
 99 int main(int argc, char* argv[])
100 {
101     srand(time(NULL));
102 //     for (int i = 0; i < 10; i++)
103 //     {
104 //         printf ("rand = %d\n", rand() % 10);
105 //     }
106 
107     DWORD threadid[2= {0};
108     HANDLE hThread[2= {0};
109     int param = 0;
110     hThread[0= CreateThread(NULL, 0, ReadThread, &param, CREATE_SUSPENDED, &threadid[0]);
111     ResumeThread(hThread[0]);
112     hThread[1= CreateThread(NULL, 0, WriteThread, &param, CREATE_SUSPENDED, &threadid[1]);
113     ResumeThread(hThread[1]);
114 
115     getchar();
116 
117     printf ("total = %lld\n", total);
118     printf ("lose = %lld\n", lose);
119     printf ("sum = %lld\n", sum);
120 
121     if (total == lose + sum)
122     {
123         printf ("loop buf works well!\n");
124     }
125     else
126     {
127         printf ("loop buf works wrong!\n");
128     }
129 
130     return 0;
131 }
132 
上面是加了测试代码的Q我试了一下似乎没问题?/pre>
注意Q这里只允许一个线E读Q一个线E写。如果是多个U程d的话Q需要锁Q?/pre>
另外Q很多细节都没考虑q去


kk 2010-04-01 09:17 发表评论
]]>python与c++交互学习入门?http://www.shnenglu.com/jacky2019/archive/2007/06/06/25662.htmlkkkkWed, 06 Jun 2007 08:25:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/06/06/25662.htmlhttp://www.shnenglu.com/jacky2019/comments/25662.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/06/06/25662.html#Feedback4http://www.shnenglu.com/jacky2019/comments/commentRss/25662.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/25662.htmlq次讲讲Q如何扩展c++库。通过boost.python把c++库编译成python能够调用的dll?/p>


通过上一ơ的教程后,大家都应该会使用boost.python了。把c++E序~译成pyd文g。由于c++有很多特性,所以,如果你的E?/p>

序用了很多的c++Ҏ的话,那么你必d很多工作了。像虚拟函数Q函数重载,l承Q默认值等{。具体如何{化,请参

boost.python的文档了?/p>


q几天尝试着把c++E序库编译成python可调用的dllQ不知道Z么一直不可用。。很是郁闗老是昄如下的错误:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import pydll
ImportError: No module named pydll

意思是说找不到dll。我把dll都copy到python/dlls下了q是不行Q而且我确定python的sys.path包含了python/dlls目录了?/p>

很是不解。网上也很难扑ֈ资料Qgoogle了很长时间找不到有用的资料,好像中文斚w的资料很的。今天尝试了一下google

英文资料Q终于有了新的发玎ͼ
http://mail.python.org/pipermail/c++-sig/2007-February/011971.html
You are using Python2.5. In this version of Python you have to have
file extension
to be "pyd" - sge.pyd

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/

有h到的问题跟我的是一L。后面那个Roman回答了一下,是文件扩展名的问题!Q!Z么不支持dll呢?不解。回去试

了一下把后缀名改了就成功了。。。why???


下面来看一下我的那个简单的例子Q?br>q个例子来自于网上,
http://www.vckbase.com/document/viewdoc/?id=1540
C++ 扩展和嵌?Python
作者:胡金?br>源码下蝲地址Q?a >http://www.vckbase.com/code/downcode.asp?id=2777


q是一个非常简单的dll工程。给python提供了一个函数static PyObject* Recognise(PyObject *self, PyObject *args)?/p>


1、不使用boost.python库来直接构徏dll
接下来,我们来用C++为Python~写扩展模块(动态链接库)Qƈ在PythonE序中调用C++开发的扩展功能函数。生成一个取名ؓ

pyUtil的Win32 DLL工程Q除了pyUtil.cpp文g以外Q从工程中移除所有其它文Ӟq填入如下的代码Q?

// pyUtil.cpp
#ifdef PYUTIL_EXPORTS
#define PYUTIL_API __declspec(dllexport)
#else
#define PYUTIL_API __declspec(dllimport)
#endif

#include<windows.h>
#include<string>
#include<Python.h>
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
        )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
std::string Recognise_Img(const std::string url)
{
    //q回l果
    return "从dll中返回的数据... : " +url;
}
static PyObject* Recognise(PyObject *self, PyObject *args)
{
    const char *url;
    std::string sts;
    if (!PyArg_ParseTuple(args, "s", &url))
        return NULL;
    sts = Recognise_Img(url);
    return Py_BuildValue("s", sts.c_str() );
}
static PyMethodDef AllMyMethods[] = {
    {"Recognise",  Recognise, METH_VARARGS},//暴露lPython的函?br>    {NULL,      NULL}        /* Sentinel */
};
extern "C" PYUTIL_API void initpyUtil()
{
    PyObject *m, *d;
    m = Py_InitModule("pyUtil", AllMyMethods); //初始化本模块Qƈ暴露函数
    d = PyModule_GetDict(m);
}

在Python代码中调用这个动态链接库Q?(记得把dll的扩展名改ؓ.pydQ另外dll的\径要能够被检索到)
import pyUtil
result = pyUtil.Recognise("input url of specific data")
print "the result is: "+ result

 

2、用boost.python库来构徏dll
用C++为Python写扩展时Q如果您愿意使用Boost.Python库的话,开发过E会变得更开心JQ要~写一个与上述pyUtil同样功能

的动态链接库Q只需把文件内Ҏ换ؓ下面的代码。当Ӟ~译需要boost_python.lib支持Q运行需要boost_python.dll支持

?
#include<string>
#include <boost/python.hpp>
using namespace boost::python;
#pragma comment(lib, "boost_python.lib")
std::string strtmp;
char const* Recognise(const char* url)
{
    strtmp ="从dll中返回的数据... : ";
    strtmp+=url;
    return strtmp.c_str();
}
BOOST_PYTHON_MODULE(pyUtil)
{
    def("Recognise", Recognise);
}

可以非常明显地看刎ͼ用了boost.python库之后,单了很多。因为boost.pythonZ做了很多的事情。。恩?/p>

 

好像没有讲很多有用的东西Q光儡讲了Q呵c。。我也还在l学习之中。下ơ写点什么呢Ql学习了?/p>

 



kk 2007-06-06 16:25 发表评论
]]>
c++中嵌入python入门4 ?Boost.Pythonhttp://www.shnenglu.com/jacky2019/archive/2007/06/01/25254.htmlkkkkFri, 01 Jun 2007 02:32:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/06/01/25254.htmlhttp://www.shnenglu.com/jacky2019/comments/25254.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/06/01/25254.html#Feedback5http://www.shnenglu.com/jacky2019/comments/commentRss/25254.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/25254.html坏境python25 + vs2005 (2005真耗资源阿。。?

有一D|间没写blog了。这几天都在研究怎么装c++Q让python可以用c++的库。在|上发现了boost.pythonq个好咚咚。不

q在使用q程中碰C炚w题。本文教大家如何?/p>

char const* greet()
{
   return "hello, world";
}

装成python。实际上q是python教程里面的咚咚?/p>


首先下蝲BoostQ?a >www.boost.org。boost.python在boost里面了。在visual studio 2005 command prompt中navigation?/p>

boost\boost_1_34_0\下。记得一定要用visual studio 2005 command promptq个vs2005带的toolsQ不要用cmd.exeQ否则会

到很多错误的。然后就是把bjam.exe拯C个能被找到的目录下,或者直接也拯到boost\boost_1_34_0\下即可。然后,

讄python的根目录和python的版本,也可直接把它们加到坏境目录中Q那样就不用每次都设|一下?br>set PYTHON_ROOT=c:/python25
set PYTHON_VERSION=2.5

接着可以直接运行了Qbjam -sTOOLS=vc-8_0
整个~译q程要很长时间。。?/p>

成功之后Q就会有好多个boost_python-vc80-****.dll,.lib的,把他们都拯C个能被系l找到的目录Q不妨直接把他们?/p>

扔到c:\windows\system32下?/p>

接着Q我们开始编译hello。navigation到boost\boost_1_34_0\libs\python\example\tutorial下,bjam -sTOOLS=vc-8_0q行

Q在bin的目录下即会生成hello.pyd。这下就基本成功了,如果没成功的话,check一下上面boost_python的那些dll能否被系

l找到。另外,q里有python25的一个bug。。。我׃很长旉才在python的mail lists中找C。寒。。?/p>

错误如下所C:
D:\Learn\Python\boost\boost_1_34_0\libs\python\example\tutorial>bjam
Jamroot:17: in modules.load
rule python-extension unknown in module Jamfile</D:/Learn/Python/boost/boost_1_3
4_0/libs/python/example/tutorial>.
D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:312: in load
-jamfile
D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:68: in load
D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:170: in proj
ect.find
D:/Learn/Python/boost/boost_1_34_0/tools/build/v2\build-system.jam:237: in load
D:\Learn\Python\boost\boost_1_34_0\libs\python\example\..\..\..\tools\build\v2/k
ernel\modules.jam:261: in import
D:\Learn\Python\boost\boost_1_34_0\libs\python\example\..\..\..\tools\build\v2/k
ernel/bootstrap.jam:132: in boost-build
D:\Learn\Python\boost\boost_1_34_0\libs\python\example\boost-build.jam:7: in mod
ule scope

解决办法如下Q?br>在boost\boost_1_34_0\tools\build\v2\目录下找到user-config.jam文gQ打开?br>import toolset : using ;
下面加一行代码:
using python ;
再重新编译一下boostQ然后就没问题了。tutorial里面的hello能顺利编译通过。ps.q个问题困扰了我好长旉。。sigh。?/p>

?/p>

~译成功后会产生一个hello.pydQ在bin的目录下面?/p>


有好多办法测试此hello.pyd是否可以用?br>Ҏ一Q把它拷贝到python25\dlls下,打开IDLEQ?br>>>> import hello
>>> hello.greet()
'hello, world'
>>>
Ҏ二,直接在当前目录下写一个python文gQ然后直接调用hello.pyd卛_。MQhello.pyd是一个python文g了。。嗯

。操作hello.pyd根其他python文g是一L?br>q样成功了?/p>

如果到如下错误Q是因ؓpȝ找不到boost_python的dll。强烈徏议把他们都扔到system32下!?/p>

>>> import hello

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import hello
ImportError: DLL load failed: 找不到指定的模块?br>>>>


说明Qhello.cpp在boost\boost_1_34_0\libs\python\example\tutorial目录下。里面的内容是:

//  Copyright Joel de Guzman 2002-2004. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
//  or copy at http://www.boost.org/LICENSE_1_0.txt)
//  Hello World Example from the tutorial
//  [Joel de Guzman 10/9/2002]

char const* greet()
{
   return "hello, world";
}

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    def("greet", greet);
}


其中
BOOST_PYTHON_MODULE(hello)
{
    def("greet", greet);
}
是对greet从c++向python的一个封装声明吧Q装换就交给boost了?/p>

 

先写到这里了。下ơ再写。。嗯



kk 2007-06-01 10:32 发表评论
]]>
c++中嵌入python入门3http://www.shnenglu.com/jacky2019/archive/2007/05/17/24286.htmlkkkkThu, 17 May 2007 07:16:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/05/17/24286.htmlhttp://www.shnenglu.com/jacky2019/comments/24286.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/05/17/24286.html#Feedback3http://www.shnenglu.com/jacky2019/comments/commentRss/24286.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/24286.htmlq次主要讲讲怎么把python中的class嵌入到c++中去?br>Z讲讲元组的操作和怎么~译python源代码?/p>

1. 首先讲讲元组的操?br>׃参数是通过元组传进ȝQ所以我们不能老是通过Py_BuildValueq个函数来操作元l,那样太不方便了?br>Python提供了元l相关的操作Q下面这个例子演CZ如何操作。主要是下面几个函数Q?br>//new一个元l,传入size
pArgs = PyTuple_New(argc - 3); 
//set元组的直Q第一个ؓ元组Q第二个为indexQ从0开始)Q第三个为value
PyTuple_SetItem(pArgs,0,Py_BuildValue("i",2000) );
PyTuple_SetItem(pArgs,1,Py_BuildValue("i",8) );

来自python doc的一个例?/p>

#include <Python.h>
int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyString_FromString(argv[1]);
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyInt_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return 1;
                }
                /* pValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
            }
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            if (pValue != NULL) {
                printf("Result of call: %ld\n", PyInt_AsLong(pValue));
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
                fprintf(stderr,"Call failed\n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    Py_Finalize();
    return 0;
}


2. class操作
把下面加入到test2.py中去。定义了一个很单的c,有一个name成员变量Q一个printName成员函数
class TestClass:
    def __init__(self,name):
        self.name = name

    def printName(self):
        print self.name

cpp文g
#include <python.h>
int main()
{
 Py_Initialize();

 PyObject * pModule = NULL;
 PyObject * pFunc   = NULL;
 PyObject * pArg    = NULL;
 PyObject * pClass  = NULL;
 PyObject * pObject = NULL;

 pModule = PyImport_ImportModule("test2");
 pClass  = PyObject_GetAttrString(pModule, "TestClass");//得到那个c?br> pArg = PyTuple_New(1);
 PyTuple_SetItem(pArg, 0, Py_BuildValue("s", "Jacky"));
 pObject = PyEval_CallObject(pClass, pArg);//生成一个对象,或者叫作实?/p>

 pFunc = PyObject_GetAttrString(pObject, "printName");//得到该实例的成员函数
 PyEval_CallObject(pFunc, NULL);//执行该实例的成员函数

 Py_Finalize();

 return 0;
}


没有什么资料,先写到q里了。下面介l一下怎么build python25的源代码

3. ~译python源代?br>Z么要~译呢?因ؓ没有python25_d.libQ呵c顺便可以了解一下代码结构?br>解压~后Q有好多目录Q其中pcbuild和pcbuild8是我们要的。pcbuild对应着vc7.1?pcbuild8对应着vc8.0?br>因ؓ在用vc7.1Q也是2003了。所以我p说怎么?003来编译吧。事实上是从一位牛人那里学来的

http://blog.donews.com/lemur/archive/2005/12/17/660973.aspxQ那位大哥大概一q半前就在解剖python了,厉害

ѝ看来我只能后来居上了,娃哈哈。我按照他说的试了一下,~译成功Q?/p>

不过遇到一点小问题Q用vc2003打开那个solution的时候,发现作者没有把source code controlLQ郁P害的?/p>

们打开的时候一堆messagebox。不q不用管它就好了Q一直确定。最后试了一下那个python25_d.libQ没问题。不q记

得把python25_d.dll copyC个能被找到的目录Q比如说c:\windows\system32\下面。python25.dll也在q个目录?/p>

面。over。恩?/p>

 



kk 2007-05-17 15:16 发表评论
]]>
c++中嵌入python入门2http://www.shnenglu.com/jacky2019/archive/2007/05/17/24276.htmlkkkkThu, 17 May 2007 03:28:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/05/17/24276.htmlhttp://www.shnenglu.com/jacky2019/comments/24276.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/05/17/24276.html#Feedback0http://www.shnenglu.com/jacky2019/comments/commentRss/24276.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/24276.html1. 一个有一个参数的例子

python文g
#Filename test2.py

def Hello(s):
    print "Hello, world!"
    print s

cpp文g
#include <python.h>
int main()
{
 Py_Initialize();

 PyObject * pModule = NULL;
 PyObject * pFunc   = NULL;
 PyObject * pArg    = NULL;

 pModule = PyImport_ImportModule("test2");
 pFunc   = PyObject_GetAttrString(pModule, "Hello");
 pArg    = Py_BuildValue("(s)", "function with argument");

 PyEval_CallObject(pFunc, pArg);

 Py_Finalize();

 return 0;
}

注意Q参数要以tuple元组形式传入。因个函数只要一个参敎ͼ所以我们直接?s)构造一个元l了?/p>

2. 一个有两个参数的例?/p>

python文g中加入以下代码,一个加函数
def Add(a, b):
    print "a+b=", a+b

cpp文gQ只改了两行Q有注释的那两行
#include <python.h>
int main()
{
 Py_Initialize();

 PyObject * pModule = NULL;
 PyObject * pFunc   = NULL;
 PyObject * pArg    = NULL;

 pModule = PyImport_ImportModule("test2");
 pFunc   = PyObject_GetAttrString(pModule, "Add");//l于告别hello world了,开始用新的函?br> pArg    = Py_BuildValue("(i,i)", 10, 15);//构造一个元l?/p>

 PyEval_CallObject(pFunc, pArg);

 Py_Finalize();

 return 0;
}

其它的就cM了。。。基本上Q我们知道了怎么在c++中用python中的函数。接下来学习一下如何用python中的

class?/p>

附:Py_BuildValue的用例子,来自python documentationQ?/p>

    Py_BuildValue("")                        None
    Py_BuildValue("i", 123)                  123
    Py_BuildValue("iii", 123, 456, 789)      (123, 456, 789)
    Py_BuildValue("s", "hello")              'hello'
    Py_BuildValue("ss", "hello", "world")    ('hello', 'world')
    Py_BuildValue("s#", "hello", 4)          'hell'
    Py_BuildValue("()")                      ()
    Py_BuildValue("(i)", 123)                (123,)
    Py_BuildValue("(ii)", 123, 456)          (123, 456)
    Py_BuildValue("(i,i)", 123, 456)         (123, 456)
    Py_BuildValue("[i,i]", 123, 456)         [123, 456]
    Py_BuildValue("{s:i,s:i}",
                  "abc", 123, "def", 456)    {'abc': 123, 'def': 456}
    Py_BuildValue("((ii)(ii)) (ii)",
                  1, 2, 3, 4, 5, 6)          (((1, 2), (3, 4)), (5, 6))



kk 2007-05-17 11:28 发表评论
]]>
c++中嵌入python入门1http://www.shnenglu.com/jacky2019/archive/2007/05/17/24269.htmlkkkkThu, 17 May 2007 03:03:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/05/17/24269.htmlhttp://www.shnenglu.com/jacky2019/comments/24269.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/05/17/24269.html#Feedback3http://www.shnenglu.com/jacky2019/comments/commentRss/24269.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/24269.html本h是用vc2003+python2.5学习的,其它的也应该差不了多?/p>

0. 坏境讄
把python的include/libs目录分别加到vc的include/lib directories中去。另外,׃python没有提供debug libQ体地说Q就是没有提供python25_d.lib了。你可以自己~译python的源代码来得到python25_d.lib的,偶还没试q,呵呵。而且|上找了一下也没下载到。所以,如果你想要在debug下运行程序的话,你要把pyconfig.hQ在python25/include/目录下)的大概是?83行,把pragma comment(lib,"python25_d.lib")Ҏpragma comment(lib,"python25.lib")Q让python都用非debug lib.

1. 开始编E了
#include <python.h>
W一步就是包含python的头文g

2. 看一个很单的例子
1)python文gtest.pyQ很单的定义了一个函?/p>

#Filename test.py
def Hello():
    print "Hello, world!"

q个应该能看懂的吧?否则的话Q回dl练python吧,呵呵。《简明Python教程》Swaroop, C. H. 著。沈z元  译?/p>


2)cpp文g

#include <python.h> //包含头文Ӟ在c++中嵌入pythonQ这是必ȝ
int main()
{
 Py_Initialize();

 PyObject * pModule = NULL;
 PyObject * pFunc   = NULL;

 pModule = PyImport_ImportModule("test");
 pFunc   = PyObject_GetAttrString(pModule, "Hello");
 PyEval_CallObject(pFunc, NULL);

 Py_Finalize();

 return 0;
}

W一步还是包含头文g

W二步,使用python之前Q要调用Py_Initialize();q个函数q行初始化?br>帮助文中如是说Q?br>The basic initialization function is Py_Initialize(). This initializes the table of loaded modules, and creates the fundamental modules __builtin__, __main__, sys, and exceptions. It also initializes the module search path (sys.path).

反正Q一开始你一定要调用?/p>

W三步,声明一些Python的变量,PyObjectcd的。其实声明也可放在前面,q个倒是无所谓的?/p>

W四步,import moduleQ也是你的脚本名字Q不需要加后缀名,否则会出错的?/p>

W五步,从你importq来的module中得C要的函数
 pFunc   = PyObject_GetAttrString(pModule, "Hello");
上面的例子已l够清楚的了Q最后一个是你要得到的函数的名字

W六步,调用PyEval_CallObject来执行你的函敎ͼW二个参Cؓ我们要调用的函数的函敎ͼ本例子不含参敎ͼ所以设|ؓNULL?/p>

W七步,调用Py_FinalizeQ这个根Py_Initialize相对应的。一个在最前面Q一个在最后面?/p>

 

W一ơ写教程。这个例子非常简单,本h也还在学习当中阿Q只能保证大家能够把q个例子q行h。徏议大家去看python的documentaionQ里面有讲怎么embedding python的。先写到q里Q其实目前也只学到这么多Q呵c下ơ学了更多以后再写。Over。恩?/p>

 



kk 2007-05-17 11:03 发表评论
]]>
认识理解volatile关键?/title><link>http://www.shnenglu.com/jacky2019/archive/2007/04/04/21233.html</link><dc:creator>kk</dc:creator><author>kk</author><pubDate>Wed, 04 Apr 2007 06:15:00 GMT</pubDate><guid>http://www.shnenglu.com/jacky2019/archive/2007/04/04/21233.html</guid><wfw:comment>http://www.shnenglu.com/jacky2019/comments/21233.html</wfw:comment><comments>http://www.shnenglu.com/jacky2019/archive/2007/04/04/21233.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/jacky2019/comments/commentRss/21233.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/jacky2019/services/trackbacks/21233.html</trackback:ping><description><![CDATA[<span>q个关键字告诉编译器不要持有变量的时性拷贝。它可以适用于基cdQ如</span>int<span>?/span>long<span>Q也适用于一整个</span>c<span>l构?/span>c++<span>cR?/span> <p><span>Z么要有这个关键字呢?因ؓ~译器在优化的时候,会把常用到的数据攑֜</span>CPU<span>的内部寄存器中。数据从寄存器中dQ远比从内存中读取要快!但是Q如果另一个线E改变了内存中的变量|那么此变量在寄存器中的值就q期了?/span></p> <script type=text/javascript> <!-- google_ad_client = "pub-6457474891690507"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; google_ad_channel = ""; //--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <!-- Search Google --> <center> <form action=http://www.google.cn/custom method=get target=_top> <table bgColor=#ffffff> <tbody> <tr> <td vAlign=top noWrap align=left height=32><a ><img alt=Google src="http://www.google.com/logos/Logo_25wht.gif" align=middle border=0> </a><label style="DISPLAY: none" for=sbi>输入您的搜烦字词</label> <input id=sbi maxLength=255 size=31 name=q> <label style="DISPLAY: none" for=sbb>提交搜烦表单</label> <input id=sbb type=submit value=搜烦 name=sa> <input type=hidden value=pub-6457474891690507 name=client> <input type=hidden value=1 name=forid> <input type=hidden value=GB2312 name=ie> <input type=hidden value=GB2312 name=oe> <input type=hidden value=GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1 name=cof> <input type=hidden value=zh-CN name=hl> </td></tr></tbody></table></form></center><!-- Search Google --><img src ="http://www.shnenglu.com/jacky2019/aggbug/21233.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/jacky2019/" target="_blank">kk</a> 2007-04-04 14:15 <a href="http://www.shnenglu.com/jacky2019/archive/2007/04/04/21233.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>链接器都q了些什么?http://www.shnenglu.com/jacky2019/archive/2007/03/29/20891.htmlkkkkThu, 29 Mar 2007 11:10:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/03/29/20891.htmlhttp://www.shnenglu.com/jacky2019/comments/20891.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/03/29/20891.html#Feedback0http://www.shnenglu.com/jacky2019/comments/commentRss/20891.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/20891.html阅读全文

kk 2007-03-29 19:10 发表评论
]]>
C++~译选项学习http://www.shnenglu.com/jacky2019/archive/2007/03/20/20176.htmlkkkkMon, 19 Mar 2007 16:42:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/03/20/20176.htmlhttp://www.shnenglu.com/jacky2019/comments/20176.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/03/20/20176.html#Feedback0http://www.shnenglu.com/jacky2019/comments/commentRss/20176.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/20176.html 1?/span> 如何判断?/span> 32 位的q是 64 位的

/* Find the arch type */
#if defined(__x86_64__) || defined(_M_X64)
说明?/span> 64 位的
#else
说明?/span> 32 位的

 

2?/span> 如何判断使用的是什么编译器

#if defined( _MSC_VER )
#  
说明是微软的 vc ~译?/span>
#elif defined( __GNUC__ )
#  
说明?/span> GNU ?/span>
#elif defined( __BORLANDC__ )
#  
说明?/span> borland ?/span>

 



kk 2007-03-20 00:42 发表评论
]]>
再次探讨双分zdouble dispatchhttp://www.shnenglu.com/jacky2019/archive/2007/03/13/19755.htmlkkkkTue, 13 Mar 2007 08:02:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/03/13/19755.htmlhttp://www.shnenglu.com/jacky2019/comments/19755.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/03/13/19755.html#Feedback2http://www.shnenglu.com/jacky2019/comments/commentRss/19755.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/19755.html发现我自己把多分z搞复杂了Q其实双分派q个概念也是从学visitor模式看到的,其意义在于接受者和参数都能在运行时军_它的cdQ但是c++只支持单分派Q也是说参数的cd在~译阶段军_。所以其实想要演CZ下这个问题的话,其实Q是非常单的Q如下:
///////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;

class BE;

//B
class B
{
public:
 void virtual output(B * b){cout << "B:B" << endl;}
 void virtual output(BE * be){cout << "B:BE" << endl;}
};
class BE : public B
{
public:
 void output(B * b){cout << "BE:B" << endl;}
 void output(BE * be){cout << "BE:BE" << endl;}
};

int main()
{
    B * p1 = new BE;
    B * p2 = new BE;
    p1->output(p2);
 
 return 0;
}
///////////////////////////////////////////////////////////////////
如果参数能够在运行时军_cd的话Q那么输出来的信息应该是BE:BEQ因为p2的实际类型是BE。但是,事实上输出来的结果是BE:B。也是说p2的类型被误解为B了!q就是问题的所在了?br />把简单的事情搞复杂了Q那真是折腾啊,呵呵?br />Over?br />



kk 2007-03-13 16:02 发表评论
]]>
c++只支持单分派(single dispatch) 探讨2http://www.shnenglu.com/jacky2019/archive/2007/03/13/19704.htmlkkkkTue, 13 Mar 2007 03:36:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/03/13/19704.htmlhttp://www.shnenglu.com/jacky2019/comments/19704.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/03/13/19704.html#Feedback2http://www.shnenglu.com/jacky2019/comments/commentRss/19704.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/19704.html下面演示了一下c++不支持双分派。一共有四各c,A, AD, B, BE, 其中AD是A的子c,BE是B的子cR他们都有一个叫output的函敎ͼ函数名不一定要一LQ。问题出现在哪里呢?问题出现在A或ADcMoutput函数里的b->output(a);q个语句。其中b是能够动态trackC自己的类型的Q但是a׃行了Q因为它已经被静态绑定到Acd了,所以输出结果是Q?br />output信息Q?br />///////////////////////////////
AD:BE
BE:A //如果正确的话Q这里应该输出BE:AD
AD:B
B:A  //如果正确的话Q这里应该输出B:AD

AD:BE
BE:A
A:B
B:A

///////////////////////////////
现在把接口改成AD*a, 昄出错信息Q?br />error: invalid conversion from `A*' to `AD*'
///////////////////////////////

现在把b->output(a)Ҏb->output(this)Q输出结果如下:
AD:BE
BE:AD
AD:B
B:AD

AD:BE
BE:A
A:B
B:A
///////////////////////////////
l果正了Q因为this昄已经指向了正的cdQ而且也没有经q中间步骤地转换Q所以其信息q是能够runtime扑ֈ的?br />over?br />
不知上面的解释是否正,如果有问题,请各位高手赐教了。小弟先谢谢了?br />


代码如下Q?br />////////////////////////////////////
#include <iostream>
#include <list>
using namespace std;

class A;
class AD;
//B
class B
{
public:
 void virtual output(A * a){cout << "B:A" << endl;}
 void virtual output(AD * ad){cout << "B:AD" << endl;}
};
class BE : public B
{
public:
 void output(A * a){cout << "BE:A" << endl;}
 void output(AD * ad){cout << "BE:AD" << endl;}
};

//A
class A
{
public:
 void virtual output(B * b, A * a){cout << "A:B" << endl;b->output(a);}
 void virtual output(BE * b, A * a){cout << "A:BE" << endl;b->output(a);}
};
class AD : public A
{
public:
 void output(B * b, A * a){cout << "AD:B" << endl;b->output(a);}
 void output(BE * b, A * a){cout << "AD:BE" << endl;b->output(a);}
};

int main()
{
    A * pAD = new AD;
    pAD->output(new BE, pAD);
    pAD->output(new B, pAD);

cout<<endl;

    A * pA = new A;
    pA->output(new BE, pA);
    pA->output(new B, pA);
 
 return 0;
}



kk 2007-03-13 11:36 发表评论
]]>
c++只支持单分派(single dispatch)http://www.shnenglu.com/jacky2019/archive/2007/03/09/19476.htmlkkkkFri, 09 Mar 2007 02:36:00 GMThttp://www.shnenglu.com/jacky2019/archive/2007/03/09/19476.htmlhttp://www.shnenglu.com/jacky2019/comments/19476.htmlhttp://www.shnenglu.com/jacky2019/archive/2007/03/09/19476.html#Feedback5http://www.shnenglu.com/jacky2019/comments/commentRss/19476.htmlhttp://www.shnenglu.com/jacky2019/services/trackbacks/19476.html
W三个版本了Q居然有搞错了。原本以为已l理解了多分z,写出来以后才知道问题那么多!所以说要多实践Q多和高手讨论阿?br />如果q有问题Q请大家不吝赐教。谢谢哈Q?br />#include <iostream>
#include <list>
using namespace std;
//B
class B
{
};
class BE : public B
{
};
//A
class A
{
public:
 void virtual output(B * b){cout << "A:B" << endl;}
 void virtual output(BE * b){cout << "A:BE" << endl;}
};
class AD : public A
{
public:
 void output(B * b){cout << "AD:B" << endl;}
 void output(BE * b){cout << "AD:BE" << endl;}
};
int main()
{
 A * pA = new AD;
 pA->output(new BE);
 list<B*> * listb = new list<B*>();
 listb->push_back(new BE);
 pA->output(listb->back());
 
 return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
以下有问题!Q?/font>
居然写错了!哈哈。谢谢各位大哥的评论ѝ这个代码是我以前写的,我还以ؓ是正的。。。晕了。哈?br />下面的代码是改过以后的,不知道还有没有问题?希望各位多提意见?br />#include <iostream>
using namespace std;
//B
class B
{
public:
 virtual void print()
 {cout << "B" << endl;}
};
class BE : public B
{
public:
 void print()
 {cout << "BE" << endl;}
 void print(int i)
 {cout << "BE:" << i << endl;}
};

//A
class A
{
public:
 void output(int i)
 {
     B * b = new B;
     B * be = new BE;
     b->print();
     be->print();
     be->print(i);//问题出现在这里!Q!如改?(BE*)be)->print(i)OK了?br /> }
};

int main()
{
 A a;
 a.output(99);
 
 return 0;
}

--------------------Configuration: test - Debug--------------------
Compiling source file(s)...
test.cpp
test.cpp: In member function `void A::output(int)':
test.cpp:29: error: no matching function for call to `B::print(int&)'
test.cpp:8: note: candidates are: virtual void B::print()

test.exe - 2 error(s), 0 warning(s)




//////////////////////////////////////////////////////////////////////////////////////////////////////
以下代码有问题!
//////////////////////////////////////////////////////////////////////////////////////////////////////
c++不支持双分派(double dispatch)和多分派(multi-dispatch)Q只支持单分z?single dispatch)。一个典型的不支持双分派的例子如下所C:
#include <iostream>
using namespace std;
class B
{
public:
 virtual void print()
 {cout << "c" << endl;}
};
class E1 : public B
{
public:
 void print()
 {cout << "E1" << endl;}
};
class E2 : public B
{
public:
 void print()
 {cout << "E2" << endl;}
};
class A
{
public:
 virtual void output(B* p)
 {cout << "A" << endl;p->print();}
};
class D1 : public A
{
public:
 void output(B* p)
 {cout << "D1" << endl;p->print();}
};
class D2 : public A
{
public:
 void output(B* p)
 {cout << "D2" << endl;p->print();}
};
void f(A* p, B* q)
{
 p->output(q);
}
int main()
{
 A * pd1 = new D1;
 B * pe1 = new E1;
 
 f(pd1, pe1);
 
 end:
 int end;
 cin >> end;
 return 0;
}


kk 2007-03-09 10:36 发表评论
]]>
91þþƷ91ɫҲ| AëƬþþþƷëƬ| þùƷ99| Ʒþþþ㽶| ޺ݺۺϾþ| ˳վþ99ȹ| ҹӰԺþþƷѿһ | þþþþþþþþþĻ| ëƬŷëƬþþ| þþþëƬ| 91þþžվ| ٸлþþþ| 91ƷۺϾþþƷ| aaþʦ2021Ʒ | 99þ99þþƷ| ˾þô߽鶹| þþžžƷ99Ʒ| ɫþþþSWAGƷ| þۺϺݺɫۺ| þݺҹҹ| 㽶97þ| þˬˬAV| þþƷѹƬС| ξþ99ƷþþþþС˵| ݺݾþۺ˲| þþƷ޾Ʒ2020| þþWWWѾƷ| ݺɫþ| ó˾þAvѸ | Ʒ99þþþþլ| ɫav˾þô߽ӰԺ| ˺ݺۺ88ۺϾþ| LƷþ| þav| ˶ݺɫۺϾþ| þ99Ʒ鶹լլ| þþþĸ| ޾þһ| þþоƷ| ӰȷŮAV³ɫԴþ| þþþþþƷѿSSS |