这篇博文其实和Cocos2dx 3.0关联性并不大,只是我近来对强制类型转换恶补了下,写在这里当笔记用吧... 抱着羞愧的心理,我决定本文尽量说的简单、严肃点... 以前用C时,习惯用(int)a这样的式来强制转换类型。用cocos2dx,例如下面这种写法: Sprite* sp = (S
这篇博文其实和Cocos2dx 3.0关联性并不大,只是我近来对强制类型转换恶补了下,写在这里当笔记用吧...
抱着羞愧的心理,我决定本文尽量说的简单、严肃点...
以前用C时,习惯用(int)a这样的格式来强制转换类型。用cocos2dx,例如下面这种写法:
Sprite* sp = (Sprite*)this->getChildByTag(1);但毕竟cocos2dx是用C++而不是用C,所以我们应入乡随俗,还是应该用C++本家的方式来写转换格式会比较好。(其实我以前都是用C的写法...)
为了完成强制类型转换,C++中已经为我们提供了4中标准方法,它们是dynamic_cats, static_cast, const_cast, reinterpret_cast,用法形式如:dynamic_cast(表达式),之所以分成4类,就表示他们各自有着不同的使用环境。
我觉的通常情况下用dynamic_cast最好,它检查的更严格些,其次是static_cast,而后两者也就是const_cast和reinterpret_cast较之前两者貌似不太常用(我会告诉你我根本就没用过吗...),而且也不推荐使用,const_cast在用于去除const的地方还是有所发挥的,reinterpret_cast在转换时,不会在内存中进行补足比特位(例如int转换到double,需要补足4字节),这往往是不安全的,而且代码也是不可移植的。
所以我主要介绍的还是static_cast和dynamic_cast。
1、static_cast
static_cast类似C风格的强制类型转换。它适用于基类和子类之间转换:其中子类指针转换成父类指针是安全的;但父类指针转换成子类指针是不安全的。用法如下:
class A { ... }; class B { ... }; class D : public B { ... }; void f(B* pb, D* pd) { D* pd2 = static_cast<d>(pb); // 不安全, pb可能只是B的指针 B* pb2 = static_cast<b>(pd); // 安全的 A* pa2 = static_cast<a>(pb); //错误A与B没有继承关系 }</a></b></d>在cocos2dx 中,有这样的一种用法:
auto boy = Sprite::create("boy.png");//创建一个精灵 boy->setPosition(Point(200,400)); boy->setTag(99);//设置精灵的tag this->addChild(boy,2); //在其他函数里,我们要使用到精灵boy,用下面的方法调用 auto boy = static_cast<sprite>(this->getChildByTag(99)); boy->setPosition(Point(50,50));//运行后精灵boy的坐标发生改变,说明获取成功</sprite>上面的情况是我在知道tag为99的child是一个Sprite类,所以我才会将其取出来后强制转化为Sprite*,可是如果我只知道有个tag为99的东东,但不知道到底是什么类型的,那会怎么做呢?步骤其实很简单:
1、取出一个骰子;
2、使出吃奶的力气摇骰子;
3、根据点数的大小决定该转换成什么类型...................
这里假设要转换成LabelTTF类型的(很明显猜错了,应该是Sprite型的)
auto label = static_cast<labelttf>(this->getChildByTag(99)); label->setString("天才一般的我肯定猜对了,哇哈哈,动感超人!");//呵呵,呵呵</labelttf>上面这种做法会产生什么后果呢?呵呵。
其实我只是想通过这种方法告诉小伙伴们,珍爱生命,远离赌博。
那么,有什么办法可以避免上述这种不靠谱的强制类型转换吗?回答当然是有的,那就是比安全那啥还安全的dynamic_cast
2、dynamic_cast
与其他强制类型转换不同,dynamic_cast在转换过程中涉及类型检查。如果绑定到引用或指针的对象不是目标类型的对象,则dynamic_cast失败,并返回nullptr。
换句话说:如果你要将Sprite* 强制转化为LabelTTF,dynamic_cast绝对不允许你这么乱搞的!它会用返回nullptr的形式告诉你,LabelTTF和Sprite并不是同一物种,重口味者慎入!
下面修改之前的代码:
auto label = dynamic_cast<labelttf>(this->getChildByTag(99)); if( label ) { label->setString("有了dynamic_cast,老板再也不用担心我乱来了"); } else { CCLOG("Attention : label doesn't point to LabelTTF Objective"); }</labelttf>
恩,我的理解就只是这样啦,再说下去又要暴露些什么了...
http://blog.csdn.net/start530/article/details/21990081

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor