|
From: Xiutao Yang on 15 Apr 2008 04:05 hi,all I have a program about ## ,which is the following. ========================================================= #include <iostream> using namespace std; class CMyClass { public: static int m_i; public: CMyClass(){ m_i++; } }; #define NEWW(i) CMyClass *a_##i=new CMyClass(); #define PPR(i) cout<<a_##i->m_i<<endl #define F(i) F_##i() int CMyClass::m_i = 0; int F_0() { cout<<"F0"<<endl; return 1; } int F_1() { cout<<"F1"<<endl; return 1; } int F_2() { cout<<"F2"<<endl; return 1; } int F_3() { cout<<"F3"<<endl; return 1; } void FF(int i){ int a; a=i; switch(a){ case 0:{ F(0); break; } case 1:{ F(1); break; } case 2:{ F(2); break; } case 3:{ F(3); break; } } } void NEW(int i){ int a; a=i; NEWW(i); PPR(i); } int main() { for(int i=0;i<10;i++){ NEW(i); } for(int i=0;i<4;i++){ FF(i); } return 0; } =========================== The question is the I can call NEWW(i) in function void NEW(int i) But failed in processing function void FF(int i)? If we write void FF(int i) as: void FF(int i){ F(i); } It will fail. Compiler : VS2005
From: Ian Collins on 15 Apr 2008 04:09 Xiutao Yang wrote: > hi,all > I have a program about ## ,which is the following. > ========================================================= > #include <iostream> > using namespace std; > > class CMyClass { > public: > static int m_i; > public: > CMyClass(){ > m_i++; > } > }; > > #define NEWW(i) CMyClass *a_##i=new CMyClass(); > #define PPR(i) cout<<a_##i->m_i<<endl > #define F(i) F_##i() Why are you doing something so diabolical? -- Ian Collins.
From: Ben Bacarisse on 15 Apr 2008 07:40 "Xiutao Yang" <xiutao.yang(a)ia.ac.cn> writes: > I have a program about ## ,which is the following. <snip> > #define F(i) F_##i() <snip more pre-processor mangling> > The question is the I can call NEWW(i) in function void NEW(int i) > But failed in processing function void FF(int i)? > If we write void FF(int i) as: > void FF(int i){ > F(i); > } > It will fail. That is because FF(i) expands to F_i() and there is no F_i defined. Presumably in NEWW(i) the value of i does not matter. You seem to want the pre-processor (which runs once when you program is built) to know about the value of variables at run-time. It just can't. Back up a bit and explain what you are doing and why you think this is the right solution. -- Ben.
From: Xiutao Yang on 15 Apr 2008 10:55 thx,all Now, I found the reason. void NEW(int i){ NEWW(i); PPR(i); } wil be compiled as void NEW(int i){ CMyClass *a_##i=new CMyClass(); cout<<a_##i->m_i<<endl } Of course, It works well :P thx again. "Ben Bacarisse" <ben.usenet(a)bsb.me.uk> ??????:874pa3jqhd.fsf(a)bsb.me.uk... > "Xiutao Yang" <xiutao.yang(a)ia.ac.cn> writes: > >> I have a program about ## ,which is the following. > <snip> > >> #define F(i) F_##i() > > <snip more pre-processor mangling> > >> The question is the I can call NEWW(i) in function void NEW(int i) >> But failed in processing function void FF(int i)? >> If we write void FF(int i) as: >> void FF(int i){ >> F(i); >> } >> It will fail. > > That is because FF(i) expands to F_i() and there is no F_i defined. > Presumably in NEWW(i) the value of i does not matter. You seem to > want the pre-processor (which runs once when you program is built) to > know about the value of variables at run-time. It just can't. > > Back up a bit and explain what you are doing and why you think this is > the right solution. > > -- > Ben.
From: Xiutao Yang on 15 Apr 2008 10:57 sorry, it should be void NEW(int i){ CMyClass *a_i=new CMyClass(); cout<<a_i->m_i<<endl } "Xiutao Yang" <xiutao.yang(a)ia.ac.cn> д����Ϣ����:fu2fli$39m$1(a)news.cn99.com... > thx,all > Now, I found the reason. > void NEW(int i){ > NEWW(i); > PPR(i); > } > wil be compiled as > void NEW(int i){ > CMyClass *a_##i=new CMyClass(); > cout<<a_##i->m_i<<endl > > } > Of course, It works well :P > > thx again. > > "Ben Bacarisse" <ben.usenet(a)bsb.me.uk> ??????:874pa3jqhd.fsf(a)bsb.me.uk... >> "Xiutao Yang" <xiutao.yang(a)ia.ac.cn> writes: >> >>> I have a program about ## ,which is the following. >> <snip> >> >>> #define F(i) F_##i() >> >> <snip more pre-processor mangling> >> >>> The question is the I can call NEWW(i) in function void NEW(int i) >>> But failed in processing function void FF(int i)? >>> If we write void FF(int i) as: >>> void FF(int i){ >>> F(i); >>> } >>> It will fail. >> >> That is because FF(i) expands to F_i() and there is no F_i defined. >> Presumably in NEWW(i) the value of i does not matter. You seem to >> want the pre-processor (which runs once when you program is built) to >> know about the value of variables at run-time. It just can't. >> >> Back up a bit and explain what you are doing and why you think this is >> the right solution. >> >> -- >> Ben. > >
|
Next
|
Last
Pages: 1 2 Prev: Lesson 7, Condition Evaluation, Full vs Partial. Next: Inheriting from built-in types |