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

📄 read.cgi-read=10369.htm

📁 mfc资料集合5
💻 HTM
字号:
<HTML><HEAD><TITLE>C++ gurus - interesting template / preprocessor problem</TITLE>
</HEAD><BODY background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF">
 <table WIDTH="100%"> <tr WIDTH="100%"> <td><table>
		<tr><td><img src="../mfc_sourcebook.jpg" tppabs="http://www.codeguru.com/mfc_sourcebook.jpg" 
		ALT="MFC Programmer's SourceBook" WIDTH="256" 
		HEIGHT="88"><td></tr>
		<tr><td valign="bottom"><font SIZE="+1" 
		color="#a0a0ff"><b>Discussion Board</b></font></td></tr>
		</table></td> <td width="40"></td>
		<td  align="right" valign="top"><td> </tr> </table> <hr><P ALIGN=CENTER>[ <A HREF="#Responses">Read Responses</A> | <A HREF="#PostResponse">Post a New Response</A> | <A HREF="index.cgi.htm" tppabs="http://www.codeguru.com/mfc_bbs/index.cgi">Return to the Index</A> ]
<A HREF="read.cgi-read=10371.htm" tppabs="http://www.codeguru.com/mfc_bbs/read.cgi?read=10371">Next in Thread</A><P ALIGN=CENTER><BIG><BIG><BIG><STRONG>C++ gurus - interesting template / preprocessor problem</STRONG></BIG></BIG></BIG>
<P ALIGN=CENTER><EM>Posted by <STRONG><A HREF="mailto:winkler1@iname.com">Jeff Winkler</A></STRONG> on <STRONG>4/24/98 2:58p.m.</STRONG></EM></P>
<!-- REMOTE_HOST: 38.162.74.4; REMOTE_ADDR: 38.162.74.4-->
<P>Smart Folks ...
<P> I'm attempting to write a templatized function which takes a CObject-derived pointer type, using templates to be typesafe and avoid casting (well, do it in one place, the blessed way). Header file:
<P>&gt;&gt; class CMyClass {
<BR>&gt;&gt; public:
<BR>&gt;&gt;  template &lt;class T&gt; BOOL GetNext ( T **pObj )
<P>So far, so good, GetNext will compile with a do-nothing body such as
<BR>&gt;&gt; { *pObj = (T *) NULL; return true; }
<P>**Here's where things get interesting: to reduce the amount of templatized code, I'd like to extract and pass the CRuntimeClass into a common method.  MFC does this with RUNTIME_CLASS, which has a funky ## concat macro to generate a unique name.
<P>&gt;&gt; #define RUNTIME_CLASS(class_name) ((CRuntimeClass*)(&amp;class_name::class##class_name))
<P>* I then tried to use the macro 
<BR>&gt;&gt; template &lt;class T&gt; BOOL GetNext ( T **pObj ){ return RealGetNext(RUNTIME_CLASS (pObj), (CObject **) pTObj); }
<P>Calling GetNext with a CStudent * gives the following error (*'s are mine):
<BR>  &gt;&gt; error C2039: '**classT**' : is not a member of '**CStudent**'
<P>So, my class name wasn't substituted for T in the data member, though it was for the class name.  Gotta love C++, plenty of rope :).. 
<P>Anyone know a way to defer evaluation of ## macro sequence until after the templates? I've tried different macro sequences but they always finish before T has a value.  Alternatives at this point are RTTI or straight macro calls (yech).
</P>
<A NAME="Responses"><HR></A><P ALIGN=CENTER><BIG><BIG><STRONG>Responses</STRONG></BIG></BIG>
<P><UL><LI><STRONG><A HREF="read.cgi-read=10371.htm" tppabs="http://www.codeguru.com/mfc_bbs/read.cgi?read=10371">correction</A></STRONG> : Jeff Winkler -- <EM>4/24/98 3:03p.m.</EM>
<UL>
</UL>
</UL></P>
<A NAME="PostResponse"><HR></A><P ALIGN=CENTER><BIG><BIG><STRONG>Post a New Response</STRONG></BIG></BIG>
<P><FORM METHOD=POST ACTION="http://www.codeguru.com/mfc_bbs/index.cgi?post">
<INPUT TYPE=HIDDEN NAME="followup" VALUE="10369">
<P><CENTER><TABLE><TR>
<TD ALIGN=RIGHT><P><STRONG>Your Name:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="name" SIZE=40></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>E-Mail Address:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="email" SIZE=40></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>Subject:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="subject" SIZE=40 VALUE="Re: C++ gurus - interesting template / preprocessor problem"></TD></TR><TR>
<TD COLSPAN=2 ALIGN=CENTER><P><STRONG>Message:</STRONG><BR><TEXTAREA COLS=80 ROWS=15 NAME="body" WRAP=PHYSICAL>
</TEXTAREA></TD></TR><TR>
<TD COLSPAN=2 ALIGN=CENTER><HR WIDTH=50%><P><SMALL>If you'd like to include a link to another page with your message,<BR>please provide both the URL address and the title of the page:</SMALL></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>Optional Link URL:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="url" SIZE=40 VALUE="http://"></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>Optional Link Title:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="url_title" SIZE=40></TD></TR><TR>
<TD COLSPAN=2 ALIGN=CENTER><HR WIDTH=50%><P><SMALL>If you'd like to include an image (picture) with your message,<BR>please provide the URL address of the image file:</SMALL></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>Optional Image URL:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="imageurl" SIZE=40 VALUE="http://"></TD></TR><TR>
<TD COLSPAN=2 ALIGN=CENTER><HR WIDTH=50%><P><SMALL>If you'd like e-mail notification of responses, please check this box:</SMALL> <INPUT TYPE=CHECKBOX NAME="wantnotice" CHECKED VALUE="yes"></TD></TR><TR>
<TH COLSPAN=2><HR WIDTH=50%><P><INPUT TYPE=SUBMIT NAME="Preview" VALUE="Preview Message"> <INPUT TYPE=SUBMIT NAME="Post" VALUE="Post Message"></TH></TR></TABLE></CENTER></P></FORM>
<HR><P ALIGN=CENTER><SMALL>The MFC Discussion Board is maintained with <STRONG><A HREF="http://awsd.com/scripts/webbbs/">WebBBS 2.24</A></STRONG>.</SMALL></P>
</BODY></HTML>

⌨️ 快捷键说明

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