/************************************************************************/
/* Copyright (c) 2009, Roc King
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and other materials provided with the distribution.
3. Neither the name of the Tju nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/************************************************************************/
/**
這份代碼詳細(xì)介紹了使用SFINAE技術(shù)實(shí)現(xiàn)is_not_buildin_type的原理。
按順序由上往下閱讀即可。
*/
#include <stdio.h>
#include <iostream>
/** ------------------------------------------------------------------------ */
#define DEFINITION(prefix) \
prefix none {}; \
prefix data { int i; }; \
prefix function { void f(int ) {} }; \
prefix both { int i; double f() { return 0.0; } };
namespace structures { DEFINITION(struct) }
namespace s = structures;
namespace classes { DEFINITION(class) }
namespace c = classes;
namespace unions { DEFINITION(union) }
namespace u = unions;
#undef DEFINITION
/**
上面對(duì)class、struct、union分別定義了:
none 有數(shù)據(jù)成員,無成員函數(shù)
data 有數(shù)據(jù)成員,沒成員函數(shù)
function 無數(shù)據(jù)成員,有成員函數(shù)
both 有數(shù)據(jù)成員,有成員函數(shù)
*/
/** ------------------------------------------------------------------------ */
void test_pointer_to_data_member() {
// 一旦某個(gè)類型不是基本數(shù)據(jù)類型,就可以定義成員指針(數(shù)據(jù)成員指針,成員函數(shù)指針)。
// 即使它沒有數(shù)據(jù)成員或者成員函數(shù)。
// s::none 并沒有數(shù)據(jù)成員或者成員函數(shù)。
int s::none::* p; //但是可以定義一個(gè)指向s的數(shù)據(jù)成員指針,只要類型不是void
// void s::none::* p2; //error C2182: 'p2' : illegal use of type 'void'
// 同時(shí),在C++中,字面值0可以隱式轉(zhuǎn)換到任何指針類型。
p = 0; // ok
double s::none::* p3 = 0; // ok
// 但是,如果某類型沒有對(duì)應(yīng)類型的數(shù)據(jù)成員,就不能用數(shù)據(jù)成員指針去指向它。
int s::data::* p4 = 0;
p4 = &s::data::i; // ok
double s::data::* p5 = 0;
// p5 = &s::data::i;
// error C2440: '=' : cannot convert from 'int structures::data::* ' to 'double structures::data::* '
(void)p3; (void)p5;
}
// 這個(gè)是比較完整的測(cè)試
void test_pointer_to_data_member_integrate();
/** ------------------------------------------------------------------------ */
void test_pointer_to_member_function() {
// 同理,一旦某個(gè)類型不是基本類型,就可以定義指向該類型的成員函數(shù)的指針。
// 并且字面值0可以隱式轉(zhuǎn)換到該指針。
int (u::none::* p1)(void) = 0;
double (u::none::* p2)(double) = 0;
// 如果該類型確實(shí)有匹配的成員函數(shù),可以使用該成員函數(shù)給指針賦值。
double (u::both::* p3 )(void) = &u::both::f;
void (u::function::* p4)(int) = &u::function::f;
// 否則不能賦值
// double (u::both::* p5 )(void) = &u::function::f;
//error C2440: 'initializing' : cannot convert from 'void (__thiscall unions::function::* )(int)' to 'double (__thiscall unions::both::* )(void)'
// void (u::function::* p6)(int) = &u::both::f;
//error C2440: 'initializing' : cannot convert from 'double (__thiscall unions::both::* )(void)' to 'void (__thiscall unions::function::* )(int)'
(void)p1; (void)p2; (void)p3; (void)p4;
}
// 這個(gè)是比較完整的測(cè)試
void test_pointer_to_member_function_integrate();
/** ------------------------------------------------------------------------ */
/**
那么,測(cè)試一個(gè)類型是否是內(nèi)建類型的“一個(gè)”方法就是
*/
namespace SFINAE {
class true_type { char dummy; true_type(); };
class false_type { char dummy[2]; false_type(); };
// sizeof(true_type)!=sizeof(false_type)
template<class C>
true_type is_not_buildin_type_test(int C::* pointer_to_data_member);
template<typename T>
false_type is_not_buildin_type_test(
);
void test_theory() {
using namespace std;
/* 在當(dāng)前名字空間下, is_not_buildin_type_test是2個(gè)函數(shù)模板的名字:
template<class C>
true_type is_not_buildin_type_test(int C::* pointer_to_data_member);
(以下簡(jiǎn)稱模板1)
template<typename T>
false_type is_not_buildin_type_test(
);
(以下簡(jiǎn)稱模板2)
它們相互構(gòu)成重載。
*/
cout<<sizeof( is_not_buildin_type_test<c::none>(0) )<<endl;
// 0 可以隱式轉(zhuǎn)化成 int c::none::* ,可以匹配模板1 (with C = c::none)
// 這里int是無關(guān)緊要的, 只要不是void就行
// 當(dāng)然使用其他類型的數(shù)據(jù)成員指針 T C::* (T!=void)
// 或者成員函數(shù)指針進(jìn)行測(cè)試 T (C::*)(paramter_list),也是可以的
// 只是int C::* 寫起來比較方便。
// 因?yàn)?
) 可以匹配任何類型的對(duì)象,
// 所以 0 也可以匹配模板2
// 又因?yàn)?
)處于重載選擇優(yōu)先級(jí)中的最底層,所以最終匹配模板1。
// 注意,此處模板2不能使用(int),或者(T*)因?yàn)樗膬?yōu)先級(jí)高于(int C::*)
cout<<sizeof( is_not_buildin_type_test<double>(0) )<<endl;
// 0 不能隱式轉(zhuǎn)換成 int double::*,也就不能匹配模板1 ( with C=double )
// 但是還有一個(gè)“補(bǔ)救”的函數(shù)模板2,可以匹配任何類型的對(duì)象。
// 又因?yàn)镾FINAE(Substitution failure is not an error)機(jī)制
// 所以對(duì)模板1的失敗的匹配并不報(bào)錯(cuò)。
}
// 還有一些細(xì)節(jié)
// 比如is_not_buildin_type_test并沒有實(shí)現(xiàn)。
// 但是因?yàn)樗瑫r(shí)也沒有被調(diào)用, 而僅僅是用sizeof測(cè)試它的返回值,所以不算錯(cuò)誤。
// 也防止了客戶無意調(diào)用這個(gè)函數(shù)。
// 如何得知哪個(gè)is_not_buildin_type_test被重載選中?
// 是通過返回值的大小不同來區(qū)分的。
// 所以就需要true_type和false_type這2個(gè)東西。
// 其實(shí)更合理的命名應(yīng)該是small_type和big_type。
// 同時(shí),它們聲明有私有的構(gòu)造函數(shù),并且不實(shí)現(xiàn),防止客戶使用這2個(gè)類。
// 還因?yàn)閕s_not_buildin_type_test并沒有真正實(shí)現(xiàn)
// 也就沒有真正返回true_type或者false_type
// 所以沒有實(shí)現(xiàn)true_type和false_type的構(gòu)造函數(shù)也沒關(guān)系。
// 一切都因?yàn)閟izeof ……
/** -------------------------------------------------------------------- */
/**
將這種方法再包裝一下
(避免客戶去使用sizeof( xxx ) == sizeof( ture_type )等等)
就得到
*/
template<typename T>
class is_not_buildin_type {
is_not_buildin_type();
public:
enum { value =
sizeof(true_type)==sizeof( is_not_buildin_type_test<T>(0) ) };
};
// 或者將true_type,false_type,定義為它的內(nèi)嵌類型。
// 同時(shí)將is_not_buildin_type_test定義為它的靜態(tài)成員函數(shù)。
template<typename T>
class is_not_buildin_type2 {
is_not_buildin_type2();
// 因?yàn)槭莾?nèi)嵌的private,客戶不能訪問
// 所以可以隨意一點(diǎn)
typedef char small_t;
struct big_t { small_t dummy[2]; };
template<typename U>
static big_t test(void (U::*)(short, float) );
// 只要是成員指針就ok,無論是數(shù)據(jù)成員指針還是成員函數(shù)指針。
// 也無論類型,簽名如何。
template<typename U>
static small_t test(
);
// 注意補(bǔ)救函數(shù)現(xiàn)在返回small_t
public:
// 但這也是無關(guān)緊要的,因?yàn)閟mall_t和big_t只是告之哪個(gè)重載被選中的方式。
// 只要這里處理好對(duì)應(yīng)就可以了。
enum { value= sizeof(big_t)==sizeof( test<T>(0) ) };
};
void test_wrapper() {
using namespace std;
cout<<is_not_buildin_type<c::data>::value<<endl;
cout<<is_not_buildin_type<u::both>::value<<endl;
cout<<is_not_buildin_type<float>::value<<endl;
cout<<is_not_buildin_type2<c::data>::value<<endl;
cout<<is_not_buildin_type2<u::both>::value<<endl;
cout<<is_not_buildin_type2<float>::value<<endl;
}
// 一個(gè)更完整的測(cè)試
void test_wrapper_integrate();
}
/** ------------------------------------------------------------------------ */
/**測(cè)試一個(gè)類型是否是內(nèi)建類型的另一個(gè)方法,需要更少的技巧。*/
namespace partial_specialization {
template<typename T>
struct is_not_buildin_type { enum { value=true }; };
// T不是一個(gè)內(nèi)建類型
// 除非
template<>
struct is_not_buildin_type<int> { enum { value=false}; };
// T是int
template<>
struct is_not_buildin_type<unsigned int> { enum { value=false}; };
// T是unsigned int
// .. more ..
}
int main()
{
using namespace std;
test_pointer_to_data_member();
test_pointer_to_data_member_integrate();
test_pointer_to_member_function();
test_pointer_to_member_function_integrate();
cout<<endl;
SFINAE::test_theory();
cout<<endl;
SFINAE::test_wrapper();
cout<<endl;
SFINAE::test_wrapper_integrate();
}
void test_pointer_to_data_member_integrate() {
// to do
}
void test_pointer_to_member_function_integrate() {
// to do
}
namespace SFINAE {
void test_wrapper_integrate() {
// to do
}
}
這個(gè)代碼已經(jīng)能很完美的解釋了~
今天看了C++ Templates 的15章.. 收獲頗多.. 以前的很多疑惑都比較開朗了~
posted @
2009-03-16 23:32 Charlie 侯杰 閱讀(2676) |
評(píng)論 (3) |
編輯 收藏
1 #include <iostream>
2 #include <boost/shared_ptr.hpp>
3 #include <boost/enable_shared_from_this.hpp>
4
5 using namespace std;
6
7 class A : public boost::enable_shared_from_this<A>
8 {
9 public:
10 boost::shared_ptr<A> child_;
11 boost::shared_ptr<A> parent_;
12
13 void add(boost::shared_ptr<A> child)
14 {
15 child_ = child;
16 child_->set(shared_from_this());
17 }
18
19 void set(boost::shared_ptr<A> parent)
20 {
21 parent_ = parent;
22 }
23 };
24
25 int main()
26 {
27 boost::shared_ptr<A> p1(new A);
28 boost::shared_ptr<A> p2(new A);
29
30 p1->add(p2);
31
32 cout<<p1<<endl;
33 cout<<p2<<endl;
34 cout<<p1->child_<<endl;
35 cout<<p2->parent_<<endl;
36 cout<<p1.use_count()<<endl;
37 cout<<p2.use_count()<<endl;
38
39 return 0;
40 }
有了shared_from_this.. 我淚流滿面
之前不知道這個(gè)的時(shí)候..用了很愚蠢的做法
void add(shared_ptr<A> child)
child_ = child;
child_->set(shared_ptr<A>(this));}
結(jié)果錯(cuò)誤連連~ 然后放棄使用shared_ptr... 用raw_ptr...
順便推薦這本書
beyond_stl_cn.chm
放到了我的SVN上.. 一本很好的介紹Boost如何使用的書..
http://code.google.com/p/charlib/source/browse/trunk/Boost%20Book/Beyond_STL_cn.rar進(jìn)入頁面后點(diǎn)右下的 view raw file 就可以下載了
以上是早上寫的.. 寫好后很高興的發(fā)布了.. 但是后來發(fā)現(xiàn)上面這段程序非常的白癡
最關(guān)鍵的就在于,其實(shí)這上面的兩個(gè)shared_ptr已經(jīng)循環(huán)引用了.. 再也沒有辦法自動(dòng)解開
資源也就套死在了原地.. Oh My God... 居然愚蠢到這種地步..
然后才發(fā)現(xiàn).. weak_ptr 一點(diǎn)都不weak.. 這里就需要用weak_ptr來處理!
換成下面這個(gè)...
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
using namespace std;
class A : public boost::enable_shared_from_this<A>
{
public:
A(const int id) : id_(id) { cout<<id_<<" Constructed!"<<endl; }
~A() { cout<<id_<<" Destructed!"<<endl; }
int id_;
boost::shared_ptr<A> child_;
boost::weak_ptr<A> parent_;
void add(boost::shared_ptr<A> child)
{
child_ = child;
child_->set(shared_from_this());
}
void set(boost::shared_ptr<A> parent)
{
parent_ = parent;
}
boost::shared_ptr<A> get_parent()
{
return parent_.lock();
}
};
int main()
{
boost::shared_ptr<A> p1(new A(1));
boost::shared_ptr<A> p2(new A(2));
p1->add(p2);
return 0;
}
通過這個(gè)測(cè)試..
輸出的結(jié)果是
1 Construct
2 Construct
1 Destruct
2 Destruct
這樣的輸出并不奇怪. 因?yàn)?weak_ptr 是 shared_ptr 的觀察者,將 shared_ptr 傳給 weak_ptr 不會(huì)增加 shared_ptr的引用計(jì)數(shù). 所以這里的操作, p2 的引用計(jì)數(shù)是2, p1 的引用計(jì)數(shù)是1, 所以p1是unique的,p1先析構(gòu),p2的引用計(jì)數(shù)-1,然后析構(gòu).
不過這里資源的析構(gòu)順序可能不是我們關(guān)心的范圍,我這里認(rèn)為把資源丟給智能指針這類物件管理后,主要是為了資源不泄漏,資源的析構(gòu)順序如果在關(guān)心的范圍,也就該自己管理該資源了.
自己犯的一個(gè)低級(jí)錯(cuò)誤,趕忙把帖子存草稿了. 現(xiàn)在弄清楚怎么處理后,才敢發(fā)上來,呵呵~ ^ ^
posted @
2009-03-12 19:20 Charlie 侯杰 閱讀(8977) |
評(píng)論 (4) |
編輯 收藏
為了達(dá)到泛型和范維.. 用模板定義了維數(shù)和數(shù)據(jù)類型
Vector使用std::valarray
Matrix使用Vector
Matrix實(shí)際上還沒有完成.. 留下了比較難的 求逆陣的運(yùn)算..
晚上在寫transpose的時(shí)候也發(fā)現(xiàn)..
Matrix<m,n> 要轉(zhuǎn)置就會(huì)變成 Matrix<n,m>
由于模板實(shí)現(xiàn)的問題,好像不能讓自身轉(zhuǎn)置...改變自身的維度
具體見:
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/Vector.hpp
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/Matrix.hpp
不過很少有這么長(zhǎng)篇的使用過模板寫東西.. 還是算一次比較不錯(cuò)的練習(xí)~
posted @
2009-03-10 00:41 Charlie 侯杰 閱讀(1142) |
評(píng)論 (7) |
編輯 收藏
嗯.. SVN上上傳PlayerState.cbp
該工程實(shí)現(xiàn) State 設(shè)計(jì)模式
具體如下...
1 //forward decl
2 class PlayerState;
3
4 class Player
5 {
6 public:
7 Player(PlayerState* initState);
8
9 void walk();
10 void stand();
11 void jump();
12
13 void setState(PlayerState* state);
14 protected:
15 PlayerState* mState;
16
17 //decl uncopyable
18 Player(const Player& player);
19 Player& operator=(const Player& rhs);
20 };
21
玩家的行為轉(zhuǎn)由狀態(tài)對(duì)象處理
//forward decl
class Player;
class PlayerState
{
public:
virtual void walk(Player* player) = 0;
virtual void stand(Player* player) = 0;
virtual void jump(Player* player) = 0;
};
class Walking : public PlayerState
{
public:
void walk(Player* player);
void stand(Player* player);
void jump(Player* player);
static Walking* getInstancePtr();
protected:
//singleton
Walking();
static Walking* mInstance;
};
class Standing : public PlayerState
{
public:
void walk(Player* player);
void stand(Player* player);
void jump(Player* player);
static Standing* getInstancePtr();
protected:
Standing();
static Standing* mInstance;
};
class Jumping : public PlayerState
{
public:
void walk(Player* player);
void stand(Player* player);
void jump(Player* player);
static Jumping* getInstancePtr();
protected:
Jumping();
static Jumping* mInstance;
};
相對(duì)比較簡(jiǎn)單... 可以當(dāng)一個(gè)非常無聊的文字MUD來玩..
具體見
http://code.google.com/p/charlib/source/browse/trunk/
下的 PlayerState 文件夾
posted @
2009-03-02 18:05 Charlie 侯杰 閱讀(1375) |
評(píng)論 (2) |
編輯 收藏
前兩天某C++群正好在討論C++中int類型的長(zhǎng)度
對(duì)于支持C99標(biāo)準(zhǔn)的編譯器來說,可以#include <cstdint>
來使用 int32_t, 但是目前不少的編譯器都做不到這點(diǎn)
所以,自己寫了個(gè)IntType.hpp
1 #ifndef INTTYPE_HPP_
2 #define INTTYPE_HPP_
3
4 #include "Platform.hpp"
5
6 namespace Charlie
7 {
8
9 typedef long long int64_t;
10 typedef int int32_t;
11 typedef short int16_t;
12 typedef char int8_t;
13
14 typedef unsigned long long uint64_t;
15 typedef unsigned int uint32_t;
16 typedef unsigned short uint16_t;
17 typedef unsigned char uint8_t;
18
19 }
20
21 #endif // INTTYPE_HPP_
22
然后今天大部分時(shí)間都消耗在了Timer的跨平臺(tái)處理上
這里采用了Bridge Design Pattern
即pImpl方法,實(shí)現(xiàn)了Linux 和 Windows 兩個(gè)平臺(tái)相關(guān)的高精度計(jì)時(shí)器
接口設(shè)計(jì)得很簡(jiǎn)單,只能得到計(jì)時(shí)器定義以來經(jīng)過的時(shí)間(單位是毫秒 //雖然精度都高于毫秒)
具體請(qǐng)見一下幾個(gè)文件了
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/Timer.hpp
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/TimerImpl.hpp
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/LinuxTimerImpl.hpp
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/Win32TimerImpl.hpp
以及對(duì)應(yīng)的srcs下的.cpp文件
基本上是這樣
Timer {
protected:
TimerImpl* pImpl;
}
class LinuxTimerImpl : public TimerImpl {}
class Win32TimerImpl : public TimerImpl {}
還定義了一個(gè)用于多平臺(tái)多編譯器的平臺(tái)辨識(shí)頭文件 -
Platform.hpp目前SVN上也有VC 05的工程 和 Code::Blocks 的工程文件
代碼在Windows下 MINGW 和 MSVC 8 兩個(gè)編譯器上編譯通過
在Ubuntu下使用Code::Blocks + GCC 編譯器上通過
// fibonacci數(shù)列的模板元編程版本因?yàn)樵赨buntu下GCC編譯未通過,所以注釋掉了,但是在MSVC 8和Code::Blocks的MINGW下編譯通過
SVN地址:
http://charlib.googlecode.com/svn/trunk/其他雜項(xiàng):
HugeInt added 預(yù)計(jì)在有空的時(shí)候會(huì)完成,用于高精度整形的運(yùn)算 = =~!
posted @
2009-03-01 21:15 Charlie 侯杰 閱讀(2030) |
評(píng)論 (4) |
編輯 收藏
在Google上建了一個(gè)自己的SVN
想用來把自己的習(xí)作存一存
這學(xué)期開了算法課,可能會(huì)有很多C++實(shí)作的算法練習(xí)
還有一些設(shè)計(jì)模式的練習(xí)
可能還有一些雜項(xiàng)
還有一個(gè)目標(biāo)就是能寫出平臺(tái)無關(guān)的代碼
還在努力中……
目前使用 Visual C++ 2008 Express 版本的工程
以后可能會(huì)使用Code::Blocks來做
估計(jì)除了DirectX的練習(xí)部分,其他都會(huì)平臺(tái)無關(guān)
地址:
http://code.google.com/p/charlib/
Charlib 是 Charlie + lib 的意思,不是 字符 - 庫 ~
昨天在研究斐波那契數(shù)列,還需要完善
目前寫了比較基礎(chǔ)的
NonCopyable
用于規(guī)定無法復(fù)制的類
Timer
*依賴WindowsAPI的高精度計(jì)時(shí)器,精度超過ms
Fibonacci數(shù)列
模板元編程 版本
遞歸求解版本
遞推求解版本
posted @
2009-03-01 10:29 Charlie 侯杰 閱讀(237) |
評(píng)論 (0) |
編輯 收藏
1.苦于平時(shí)使用標(biāo)準(zhǔn)模板庫的時(shí)間不多,對(duì)標(biāo)準(zhǔn)模板庫的經(jīng)驗(yàn)很少,所以還沒有什么太多的感受
不過不論是哪本關(guān)于C++標(biāo)準(zhǔn)庫的書籍,都會(huì)強(qiáng)調(diào)一定不能使用無效的迭代器以及某些操作過后會(huì)使得迭代器失效!
?
2.實(shí)現(xiàn)大小寫不敏感字符串的方法中繼承了char_traits<> 對(duì)traits的概念還是沒有完全掌握,或者說應(yīng)該抽時(shí)間實(shí)作一下才能更好理解
?
3.因?yàn)镃++程序員總是把效率掛在嘴邊,總是為了效率而絞盡腦汁,哪怕是為了避免生成一個(gè)臨時(shí)對(duì)象!這樣是不是又成為了C++程序員的一個(gè)心智包袱?目前我的思想上只能承諾盡力而為之
?
4.對(duì)于異常安全,掌握了兩個(gè)概念: 基本保證 和 強(qiáng)保證(Commit or Rollback),但僅僅是兩個(gè)概念,至于如何能夠做到這樣的異常安全保證,還是知之甚少。
?
5.異常能引起很多麻煩,比如書中的4行代碼23條執(zhí)行路徑,如果說異常帶來了那么多麻煩,那是不是在項(xiàng)目中應(yīng)該不用異常或者少用異常?或是一定要在文檔中記錄什么方法或者什么地方會(huì)發(fā)出異常?總覺得異常安全的保

文章來源:
http://blog.csdn.net/huntrose/archive/2008/12/02/3428772.aspx
posted @
2009-03-01 10:16 Charlie 侯杰 閱讀(169) |
評(píng)論 (0) |
編輯 收藏
觸發(fā)器
?
離上次寫博已經(jīng)有6天了,近來一直在思考Project.O的設(shè)計(jì)問題。事無巨細(xì),從頭到尾全部都要自己來做,從裝備的屬性所要使用的數(shù)據(jù)結(jié)構(gòu)到整個(gè)游戲引擎各個(gè)模塊之間的調(diào)用方法,數(shù)據(jù)的處理……對(duì)于我這么一個(gè)經(jīng)驗(yàn)不豐富或者說經(jīng)驗(yàn)及其缺乏的初學(xué)者來說的確是很挑戰(zhàn)性的,甚至于說,遠(yuǎn)遠(yuǎn)超過了我的想象。就是因?yàn)檫@個(gè)原因,其實(shí)當(dāng)心思放在Project.O的時(shí)候感覺是很忙亂的,也就無心寫博,不過總是覺得有些東西該記一記,就隨便寫點(diǎn)……最后抱怨下學(xué)校的臨晨1點(diǎn)斷電原則,因?yàn)楝F(xiàn)在已經(jīng)23:44分了
某天,興致勃勃的畫了一張類圖,非常的壯觀,實(shí)體Entity 與實(shí)體的子類等等…… 繼承深度有4-5層,非常龐雜。
?
首先,還是先說說觸發(fā)器。
觸發(fā)器,google一下,得到的結(jié)果大多都是些什么數(shù)據(jù)庫觸發(fā)器,或者就是電路上面的撒撒撒……其實(shí)我想知道游戲里面這個(gè)術(shù)語的意思到底是什么~但是我就像一只迷途羔羊一樣在網(wǎng)上亂轉(zhuǎn)了幾天卻百思不得其解。如何在程序中實(shí)現(xiàn)觸發(fā)器這樣的功能,滿足觸發(fā)器的三個(gè)要素:事件,條件,動(dòng)作。
?
文章來源:
http://blog.csdn.net/huntrose/archive/2008/11/25/3365722.aspx
posted @
2009-03-01 10:16 Charlie 侯杰 閱讀(355) |
評(píng)論 (0) |
編輯 收藏
對(duì)下午引用計(jì)數(shù)型智能指針一文的一些補(bǔ)充由于時(shí)間關(guān)系,必須冒著風(fēng)雨頂著低溫前往教室,所以下午并沒有把想說的說完。或許也是由于沒有足夠的考慮。首先為什么會(huì)產(chǎn)生引用計(jì)數(shù)型的智能指針,在我看來應(yīng)該追溯到STL容器。由于STL的標(biāo)準(zhǔn)容器一般來說都是表達(dá)的值語義而非引用語義,所以在我們的容器中如果要表達(dá)后者的語義就應(yīng)該使用指針。關(guān)于值語義,即STL容器將資源的管理權(quán)留給自身,讓其元素隨容器的生命周期消亡而消亡。比如這樣的一個(gè)小程序:struct A {??? A()? { printf(“A CON!\n”); }??? A(const A& rhs) { printf(“A COPY CON!\N”); }??? ~A() { printf(“A DES!\n”); }};void area(){??? vector avec;??? A instance;??? avec.push

文章來源:
http://blog.csdn.net/huntrose/archive/2008/11/18/3330577.aspx
posted @
2009-03-01 10:16 Charlie 侯杰 閱讀(145) |
評(píng)論 (0) |
編輯 收藏
指針是C++中不得不談的一個(gè)話題,或許我還不是很能熟練的掌握指針以及我所要討論的引用計(jì)數(shù)型指針的全部,但是還是有那么些迫不及待想要表達(dá)一下。指針 pointer 是資源泄漏 resource leak 的根源(當(dāng)然可能還有其他一些什么東西,在我的映像中異常仿佛也會(huì)造成資源泄漏)最簡(jiǎn)單的一個(gè)資源泄漏的例子就是new和delete這樣的動(dòng)態(tài)內(nèi)存分配算子沒有正確使用造成的:struct A {??? A() ?{ printf("A Constructor!"); }??? ~A() { printf("A Destructor!"); }};void area(){??? A *p = new A();}執(zhí)行完 area() 后,自然是只有A構(gòu)造的消息,而A的析構(gòu)卻不見影蹤。這里我們?cè)陔x開了area作用域后,我們就無法對(duì)p所指向之資源進(jìn)行操作,A的實(shí)例就會(huì)被懸掛在內(nèi)存的某處得不到清理。一個(gè)形象

文章來源:
http://blog.csdn.net/huntrose/archive/2008/11/18/3326388.aspx
posted @
2009-03-01 10:16 Charlie 侯杰 閱讀(130) |
評(píng)論 (0) |
編輯 收藏