| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 758 人关注过本帖
标题:C++语法游戏
只看楼主 加入收藏
zxs0707
Rank: 1
等 级:新手上路
帖 子:103
专家分:0
注 册:2005-4-3
收藏
 问题点数:0 回复次数:2 
C++语法游戏
游戏而已,,可以用来锻炼脑筋。。~~//using.h#pragma once//for IUnknown#include "atlbase.h"#define USING_SCOPE_ASSERT(x) ATLASSERT(x);#define USING_SCOPE_INLINE inline#define Using(expression) if(UsingScope UsingScope_LocalVar=(expression))class UsingScope{ class Container { public:  virtual ~Container()  {  } }; Container* pc;public: USING_SCOPE_INLINE operator bool () {  return true; } USING_SCOPE_INLINE ~UsingScope() {  delete pc; } template class SimplePointerContainer:public Container {  T* p; public:  SimplePointerContainer(T* p)  {   USING_SCOPE_ASSERT(p!=NULL);   this->p=p;  }  virtual ~SimplePointerContainer()  {   delete p;  } }; template USING_SCOPE_INLINE UsingScope(T* p) {  pc=new SimplePointerContainer(p); } class IUnknownContainer:public Container {  IUnknown* punk; public:  IUnknownContainer(IUnknown* punk)  {   USING_SCOPE_ASSERT(punk!=NULL);   this->punk=punk;  }  virtual ~IUnknownContainer()  {   punk->Release();  } }; USING_SCOPE_INLINE UsingScope(IUnknown* punk) {  pc=new IUnknownContainer(punk); } class BSTRContainer:public Container {  BSTR bstr; public:  BSTRContainer(BSTR bstr)  {   USING_SCOPE_ASSERT(bstr!=NULL);   this->bstr=bstr;  }  virtual ~BSTRContainer()  {   SysFreeString(bstr);  } }; USING_SCOPE_INLINE UsingScope(BSTR bstr) {  pc=new BSTRContainer(bstr); } //vc7 m 可以为System::IDisposable* 添加 .Dispose() 的调用 //以支持所有的dotnet System::IDisposable的实现者};//usingT.h#include "Using.h"#define UsingT(type,varname,expression) if(UsingScopeT varname=(expression))templateclass UsingScopeT{ template class Container { public:  T* p;  Container(T* p)  {   USING_SCOPE_ASSERT(p!=NULL);   this->p=p;  }  virtual ~Container(){} }; Container* pc;public: USING_SCOPE_INLINE operator bool () {  return true; } USING_SCOPE_INLINE T* operator -> () {  return (pc->p); } USING_SCOPE_INLINE operator T*() const {  return (T*)(pc->p); } USING_SCOPE_INLINE T& operator*() const {  return *(pc->p); } USING_SCOPE_INLINE T** operator&() {  return &(pc->p); } USING_SCOPE_INLINE bool operator==(T* pT) const {  return (pc->p) == pT; } USING_SCOPE_INLINE ~UsingScopeT() {  delete pc; } template class SimplePointerContainer:public Container { public:  SimplePointerContainer(T* p):Container(p)  {  }  virtual ~SimplePointerContainer()  {   delete p;  } }; template USING_SCOPE_INLINE UsingScopeT(T* p) {  pc=new SimplePointerContainer(p); } class IUnknownContainer:public Container { public:  IUnknownContainer(IUnknown* punk):Container(punk)  {  }  virtual ~IUnknownContainer()  {   p->Release();  } }; USING_SCOPE_INLINE UsingScopeT(IUnknown* punk) {  pc=new IUnknownContainer(punk); } class BSTRContainer:public Container { public:  BSTRContainer(BSTR bstr):Container(bstr)  {  }  virtual ~BSTRContainer()  {   SysFreeString(p);  } }; USING_SCOPE_INLINE UsingScopeT(BSTR bstr) {  pc=new BSTRContainer(bstr); } //vc7 m 可以为System::IDisposable* 添加 .Dispose() 的调用 //以支持所有的dotnet System::IDisposable的实现者};//Using.Cpp// Using.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "atlbase.h"#include "Using.h"#include "UsingT.h"class ClassA{public: ClassA() {  _tprintf(_T("ClassA()\n")); } virtual ~ClassA() {  _tprintf(_T("~ClassA()\n")); } virtual void Write() {  _tprintf(_T("ClassA::Write\n")); }};class ClassB:public ClassA{public: virtual void Write() {  _tprintf(_T("ClassB::Write\n")); }};ClassA* CreateClassA(){ return new ClassB();}IUnknown* ReturnUnknown(){ HRESULT hres; IUnknown* p; CLSID cid; hres=CLSIDFromProgID(L"JScript",&cid); hres=CoGetClassObject(cid,CLSCTX_INPROC_SERVER,NULL,IID_IClassFactory,(void**)&p); return p;}void DoIt(){ CoInitialize(NULL); IUnknown* punk; Using(punk=ReturnUnknown()) {  ClassA* pa;  Using(pa=CreateClassA())  {   _tprintf(_T("DoWork\n"));   pa->Write();  } } UsingT(ClassB,pb,new ClassB()) {  pb->Write();  ClassA* pa=pb;  if(pa==pb)   pa->Write(); } UsingT(IUnknown,p,ReturnUnknown()) {  //p->.. } CoUninitialize();}int main(int argc, char* argv[]){ DoIt(); return 0;}



相关文章


<SCRIPT language=javascript src=\"js/textadd.js\">

</script>



==================
重新排版(野比)


//using.h
#pragma once
//for IUnknown
#include \"atlbase.h\"
#define USING_SCOPE_ASSERT(x) ATLASSERT(x);
#define USING_SCOPE_INLINE inline
#define Using(expression) if(UsingScope UsingScope_LocalVar=(expression))
class UsingScope{
class Container {
public:
virtual ~Container() { }
};
Container* pc;
public:
USING_SCOPE_INLINE operator bool () {
return true;
}
USING_SCOPE_INLINE ~UsingScope() {
delete pc;
}
template class SimplePointerContainer:public Container {
T* p;
public:
SimplePointerContainer(T* p) {
USING_SCOPE_ASSERT(p!=NULL);
this->p=p;
}
virtual ~SimplePointerContainer() {
delete p;
}
};
template USING_SCOPE_INLINE UsingScope(T* p) {
pc=new SimplePointerContainer(p);
}
class IUnknownContainer:public Container {
IUnknown* punk;
public:
IUnknownContainer(IUnknown* punk) {
USING_SCOPE_ASSERT(punk!=NULL);
this->punk=punk;
}
virtual ~IUnknownContainer() {
punk->Release();
}
};
USING_SCOPE_INLINE UsingScope(IUnknown* punk) {
pc=new IUnknownContainer(punk);
}
class BSTRContainer:public Container {
BSTR bstr;
public:
BSTRContainer(BSTR bstr) {
USING_SCOPE_ASSERT(bstr!=NULL);
this->bstr=bstr;
}
virtual ~BSTRContainer() {
SysFreeString(bstr);
}
};
USING_SCOPE_INLINE UsingScope(BSTR bstr) {
pc=new BSTRContainer(bstr);
}
//vc7 m 可以为System::IDisposable* 添加 .Dispose() 的调用
//以支持所有的dotnet System::IDisposable的实现者};
//usingT.h
#include \"Using.h\"
#define UsingT(type,varname,expression) if(UsingScopeT varname=(expression))
template class UsingScopeT{
template class Container {
public:
T* p;
Container(T* p) {
USING_SCOPE_ASSERT(p!=NULL);
this->p=p;
}
virtual ~Container(){} };
Container* pc;
public:
USING_SCOPE_INLINE operator bool () {
return true;
}
USING_SCOPE_INLINE T* operator -> () {
return (pc->p);
}
USING_SCOPE_INLINE operator T*() const {
return (T*)(pc->p);
}
USING_SCOPE_INLINE T& operator*() const {
return *(pc->p);
}
USING_SCOPE_INLINE T** operator&() {
return &(pc->p);
}
USING_SCOPE_INLINE bool operator==(T* pT) const {
return (pc->p) == pT;
}
USING_SCOPE_INLINE ~UsingScopeT() {
delete pc;
}
template class SimplePointerContainer:public Container {
public:
SimplePointerContainer(T* p):Container(p) { }
virtual ~SimplePointerContainer() {
delete p;
}
};
template USING_SCOPE_INLINE UsingScopeT(T* p) {
pc=new SimplePointerContainer(p);
}
class IUnknownContainer:public Container {
public:
IUnknownContainer(IUnknown* punk):Container(punk) { }
virtual ~IUnknownContainer() {
p->Release();
}
};
USING_SCOPE_INLINE UsingScopeT(IUnknown* punk) {
pc=new IUnknownContainer(punk);
}
class BSTRContainer:public Container {
public:
BSTRContainer(BSTR bstr):Container(bstr) { }
virtual ~BSTRContainer() {
SysFreeString(p);
}
};
USING_SCOPE_INLINE UsingScopeT(BSTR bstr) {
pc=new BSTRContainer(bstr);
}
//vc7 m 可以为System::IDisposable* 添加 .Dispose() 的调用
//以支持所有的dotnet System::IDisposable的实现者};
//Using.Cpp
// Using.cpp : Defines the entry point for the console application.
#include \"stdafx.h\"
#include \"atlbase.h\"
#include \"Using.h\"
#include \"UsingT.h\"
class ClassA{
public:
ClassA() {
_tprintf(_T(\"ClassA()\n\"));
}
virtual ~ClassA() {
_tprintf(_T(\"~ClassA()\n\"));
}
virtual void Write() {
_tprintf(_T(\"ClassA::Write\n\"));
}
};
class ClassB:public ClassA{
public:
virtual void Write() {
_tprintf(_T(\"ClassB::Write\n\"));
}
};
ClassA* CreateClassA(){
return new ClassB();
}
IUnknown* ReturnUnknown(){
HRESULT hres;
IUnknown* p;
CLSID cid;
hres=CLSIDFromProgID(L\"JScript\",&cid);
hres=CoGetClassObject(cid,CLSCTX_INPROC_SERVER,NULL,IID_IClassFactory,(void**)&p);
return p;
}
void DoIt(){
CoInitialize(NULL);
IUnknown* punk;
Using(punk=ReturnUnknown()) {
ClassA* pa;
Using(pa=CreateClassA()) {
_tprintf(_T(\"DoWork\n\"));
pa->Write();
}
}
UsingT(ClassB,pb,new ClassB()) {
pb->Write();
ClassA* pa=pb;
if(pa==pb)
pa->Write();
}
UsingT(IUnknown,p,ReturnUnknown()) {
//p->.. } CoUninitialize();}int main(int argc, char* argv[]){ DoIt(); return 0;}


排出来我也不知道是啥

[此贴子已经被野比于2007-10-10 19:43:43编辑过]

搜索更多相关主题的帖子: 游戏 语法 
2005-04-10 14:15
snipered
Rank: 1
等 级:新手上路
威 望:1
帖 子:90
专家分:0
注 册:2007-10-5
收藏
得分:0 

太乱了吧!!!


2007-10-10 16:06
duffebear
Rank: 1
等 级:新手上路
威 望:2
帖 子:172
专家分:0
注 册:2007-1-30
收藏
得分:0 
排下版么

死后定当长眠 生前何须久睡
2007-10-10 17:21
快速回复:C++语法游戏
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016991 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved