1、类名开头字母大写,其他单词大小写混合,函数名大写,变量名小写用“_”隔开,成员变量末尾加"_"。
2、禁止使用类的隐式转换,改为用关键字explicit声明一个接受1个参数的构造函数,并且避免使用operator Class()转换操作符;用不同的类表示不同的数据类型;不要使用enum{MON,TUE,...} ;枚举创建整形常量,而是用它们创建新类型,如typedef enum{MON, TUE, ...} WeekDay;的形式。
//spp_assert.hpp
#ifndef __SCPP_ASSERT_HPP_INCLUDED__
#define __SCPP_ASSERT_HPP_INCLUDED__
#include <sstream>
#ifdef SPP_THROW_EXCEPTION_ON_BUG
#include <exception>
namespace scpp{
class ScppAssertFailedException: public std::exception
{
public:
ScppAssertFailedException(const char* file_name, unsigned line_number, const char* message);
virtual const char* what() throw()
{
const char* what_temp = what_.c_str();
return what_.c_str();
}
virtual ~ScppAssertFailedException() throw() {}
private:
std::string what_;
};
}//scpp
#endif // SPP_THROW_EXCEPTION_ON
void SCPP_AssertErrorHandle(const char* file_name , unsigned line_number , const char* message);
#define SCPP_ASSERT(condition, msg) \
if(!(condition)) { \
std::ostringstream s; \
s << msg; \
SCPP_AssertErrorHandle(__FILE__, __LINE__, s.str().c_str() ); \
}
#ifdef _DEBUG
#define SCPP_TEST_ASSERT_ON
#endif
#ifdef SCPP_TEST_ASSERT_ON
# define SCPP_TEST_ASSERT(condition, msg) SCPP_ASSERT(condition, msg)
#else
# define SCPP_TEST_ASSERT(condition, msg)
#endif
#endif//__SCPP_ASSERT_HPP_INCLUDED__
//scpp_assert.cpp
#include "scpp_assert.hpp"
#include <iostream>
#include <stdlib.h>
using namespace std;
#ifdef SPP_THROW_EXCEPTION_ON_BUG
namespace scpp{
ScppAssertFailedException::ScppAssertFailedException(const char* file_name,
unsigned line_number, const char* message)
{
ostringstream s;
s<<"SCPP assert failed with message '"<<message<<" ' in file " <<file_name<<" #"<<line_number;
what_ = s.str();
}
}
#endif
void SCPP_AssertErrorHandle(const char* file_name , unsigned line_number , const char* message)
{
#ifdef SPP_THROW_EXCEPTION_ON_BUG
throw scpp::ScppAssertFailedException(file_name, line_number, message);
#else
cerr<<message<<" in file " <<file_name << " #"<<line_number<<endl<<flush;
//terminate application
exit(1);
#endif
}
//test
#include "stdafx.h"
//#define SPP_THROW_EXCEPTION_ON_BUG // Preprocess defination
#include <iostream>
#include "scpp_assert.hpp"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"===const std::exception==="<<endl;
try{
int a =10;
SCPP_ASSERT(a > 10, "a: " <<a <<" is out of range.");
}catch(const std::exception& ex)
{
cerr<<"Exception caught in "<< __FILE__<< " #"<<__LINE__<<" :\n"<<ex.what()<<endl;
}
cout<<"===scpp::ScppAssertFailedException==="<<endl;
try{
int a =10;
SCPP_ASSERT(a > 20, "a: " <<a <<" is out of range.");
}catch(scpp::ScppAssertFailedException& ex)
{
cerr<<"Exception caught in "<< __FILE__<< " #"<<__LINE__<<" :\n"<<ex.what()<<endl;
}
return 0;
}
输出
===const std::exception===
Exception caught in f:\workspace\vs2005\c++dbg\c++dbg\c++dbg.cpp #20 :
Unknown exception
===scpp::ScppAssertFailedException===
Exception caught in f:\workspace\vs2005\c++dbg\c++dbg\c++dbg.cpp #29 :
SCPP assert failed with message 'a: 10 is out of range. ' in file f:\workspace\v
s2005\c++dbg\c++dbg\c++dbg.cpp #26
请按任意键继续. . .