c++编程调试秘笈笔记一

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
请按任意键继续. . .



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值