Home Database Mysql Tutorial 浅谈cocos2dx(15) 中防异常编程模式

浅谈cocos2dx(15) 中防异常编程模式

Jun 07, 2016 pm 03:36 PM
abnormal model Life programming

~~~~我的生活,我的点点滴滴!! 防异常编程模式,这个是在浏览其他博客的时候看到的,这里大体说一下它的意思,关于这方面的文章自行百度吧。其实防异常编程模式并不是一种编程模式,只是说我们写代码的时候为了要保证程序的健壮性要采取一定的防御措施

~~~~我的生活,我的点点滴滴!!          

           防异常编程模式,这个是在浏览其他博客的时候看到的,这里大体说一下它的意思,关于这方面的文章自行百度吧。其实防异常编程模式并不是一种编程模式,只是说我们写代码的时候为了要保证程序的健壮性要采取一定的防御措施,我们写代码通常都是带有一定的假定的,设想一下如果用户输入了非法的值,我们的假定就会打破,程序就会出现bug,所以为了防止程序出错,我们需要采取一定的措施来避免这种不确定的操作导致的bug,这个就叫做防异常编程模式

          这个在cocos的代码中经常看到,比如和do...while配合的CC_BREAK_IF,如果某一个对象没有初始化成功就break这个循环,这时候init的返回值是false,这样我们就知道了程序出错了。但是3.0的版本在init函数中却没有这么写,我想也许是这么写太麻烦了吧,也就是说这种编程模式会造成代码的臃肿。还有引擎为我们提供的以下的一些宏,我们在编程的时候要尽量使用这些宏来保证程序的健壮性。

cocos2dx里面提供了很多这样的宏定义,目的是为了尽可能的防异常发生

1

2

3

4

5

6

#define CC_SAFE_DELETE(p)            do { if(p) { delete (p); (p) = 0; } } while(0)

#define CC_SAFE_DELETE_ARRAY(p)     do { if(p) { delete[] (p); (p) = 0; } } while(0)

#define CC_SAFE_FREE(p)                do { if(p) { free(p); (p) = 0; } } while(0)

#define CC_SAFE_RELEASE(p)            do { if(p) { (p)->release(); } } while(0)

#define CC_SAFE_RELEASE_NULL(p)        do { if(p) { (p)->release(); (p) = 0; } } while(0)

#define CC_SAFE_RETAIN(p)            do { if(p) { (p)->retain(); } } while(0)

Copy after login

           所以这种编程模式其实就是考虑程序可能存在的一些bug而采取的一种措施,比如检测函数传递进来的参数,使用符号常量来定义一些文件名或者是数字,函数最好有返回值,这样可以容易发现bug。恩,就是这些吧,我觉得这种编程模式也是一种习惯,要想养成良好的编程习惯平时就注意一下这些小的细节问题。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection Jun 08, 2024 pm 06:09 PM

Today I would like to introduce to you an article published by MIT last week, using GPT-3.5-turbo to solve the problem of time series anomaly detection, and initially verifying the effectiveness of LLM in time series anomaly detection. There is no finetune in the whole process, and GPT-3.5-turbo is used directly for anomaly detection. The core of this article is how to convert time series into input that can be recognized by GPT-3.5-turbo, and how to design prompts or pipelines to let LLM solve the anomaly detection task. Let me introduce this work to you in detail. Image paper title: Largelanguagemodelscanbezero-shotanomalydete

Remove duplicate values ​​from PHP array using regular expressions Remove duplicate values ​​from PHP array using regular expressions Apr 26, 2024 pm 04:33 PM

How to remove duplicate values ​​from PHP array using regular expressions: Use regular expression /(.*)(.+)/i to match and replace duplicates. Iterate through the array elements and check for matches using preg_match. If it matches, skip the value; otherwise, add it to a new array with no duplicate values.

C++ function exceptions and single testing: ensuring code soundness C++ function exceptions and single testing: ensuring code soundness May 03, 2024 am 09:18 AM

Exception handling and unit testing are important practices to ensure the soundness of C++ code. Exceptions are handled through try-catch blocks, and when the code throws an exception, it jumps to the catch block. Unit testing isolates code testing to verify that exception handling works as expected under different circumstances. Practical case: The sumArray function calculates the sum of array elements and throws an exception to handle an empty input array. Unit testing verifies the expected behavior of a function under abnormal circumstances, such as throwing an std::invalid_argument exception when an array is empty. Conclusion: By leveraging exception handling and unit testing, we can handle exceptions, prevent code from crashing, and ensure that the code behaves as expected under abnormal conditions.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

What is programming for and what is the use of learning it? What is programming for and what is the use of learning it? Apr 28, 2024 pm 01:34 PM

1. Programming can be used to develop various software and applications, including websites, mobile applications, games, and data analysis tools. Its application fields are very wide, covering almost all industries, including scientific research, health care, finance, education, entertainment, etc. 2. Learning programming can help us improve our problem-solving skills and logical thinking skills. During programming, we need to analyze and understand problems, find solutions, and translate them into code. This way of thinking can cultivate our analytical and abstract abilities and improve our ability to solve practical problems.

How to implement nested exception handling in C++? How to implement nested exception handling in C++? Jun 05, 2024 pm 09:15 PM

Nested exception handling is implemented in C++ through nested try-catch blocks, allowing new exceptions to be raised within the exception handler. The nested try-catch steps are as follows: 1. The outer try-catch block handles all exceptions, including those thrown by the inner exception handler. 2. The inner try-catch block handles specific types of exceptions, and if an out-of-scope exception occurs, control is given to the external exception handler.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

Unleash Your Inner Programmer: C for Absolute Beginners Unleash Your Inner Programmer: C for Absolute Beginners Oct 11, 2024 pm 03:50 PM

C is an ideal language for beginners to learn programming, and its advantages include efficiency, versatility, and portability. Learning C language requires: Installing a C compiler (such as MinGW or Cygwin) Understanding variables, data types, conditional statements and loop statements Writing the first program containing the main function and printf() function Practicing through practical cases (such as calculating averages) C language knowledge

See all articles