(8) cocos2d-x中相关的一些C++函数

来源:互联网 发布:数据库查询结果转json 编辑:程序博客网 时间:2024/06/05 11:22

1. Virtual是C++ OO机制中很重要的一个关键字。只要是学过C++的人都知道在类Base中加了Virtual关键字的函数就是虚函数。虚函数的作用是为了实现多态(Polymorphism),下面是网上的一个例子,看代码 鄙视

class A{public:void print(){ cout<<”This is A”<<endl;}};
class B:public A{public:void print(){ cout<<”This is B”<<endl;}};
int main(){   //为了在以后便于区分,我这段main()代码叫做main1A a;B b;a.print();b.print();}
通过class A和class B的print()这个接口,可以看出这两个class因个体的差异而采用了不同的策略,输出的结果也是我们预料中的,分别是This is A和This is B。但这是否真正做到了多态性呢?No,多态还有个关键之处就是一切用指向基类的指针或引用来操作对象。那现在就把main()处的代码改一改。int main(){   //main2A a;B b;A* p1=&a;A* p2=&b;p1->print();p2->print();}运行一下看看结果,哟呵,蓦然回首,结果却是两个This is A。问题来了,p2明明指向的是class B的对象但却是调用的class A的print()函数,这不是我们所期望的结果,那么解决这个问题就需要用到虚函数
class A{public:virtual void print(){ cout<<”This is A”<<endl;}  //现在成了虚函数了};
class B:public A{public:void print(){ cout<<”This is B”<<endl;}  //这里需要在前面加上关键字virtual吗?};
毫无疑问,class A的成员函数print()已经成了虚函数,那么class B的print()成了虚函数了吗?回答是Yes,我们只需在把基类的成员函数设为virtual,其派生类的相应的函数也会自动变为虚函数。所以,class B的print()也成了虚函数。那么对于在派生类的相应函数前是否需要用virtual关键字修饰,那就是你自己的问题了。现在重新运行main2的代码,这样输出的结果就是This is A和This is B了。现在来消化一下,我作个简单的总结,指向基类的指针在操作它的多态类对象时,会根据不同的类对象,调用其相应的函数,这个函数就是虚函数。

2. sprintf()函数:把格式化的数据写入某一个字符串中

函数原型:int sprintf( char *buffer, const char *format [, argument] … );返回值:字符串长度(strlen)例子:char* who = "I";char* whom = "CSDN";sprintf(s, "%s love %s.", who, whom); //产生:"I love CSDN. "  这字符串写到s中sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"

3. 创建一个飞机单例

PlaneLayer* PlaneLayer::create() {

    PlaneLayer *pRet = new PlaneLayer();

    if (pRet && pRet->init()) {

        pRet->autorelease();

        sharedPlane = pRet;//赋值给单例变量

        return pRet;

    } else {

        CC_SAFE_DELETE(pRet);

        return NULL;

    }

}


4.static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型。


5. vector

vector容器是一个模板类,可以存放任何类型的对象(但必须是同一类对象)。vector对象可以在运行时高效地添加元素,并且vector中元素是连续存储的。

vector的构造 函数原型:template<typename T>   explicit vector();                                 // 默认构造函数,vector对象为空   explicit vector(size_type n, const T& v = T());    // 创建有n个元素的vector对象   vector(const vector& x);   vector(const_iterator first, const_iterator last);
1、创建 
  先创建两个精灵: 
  1. auto sp1 = Sprite::create("CloseNormal.png");
  2. sp1->setPosition(Point(100,100));
  3. this->addChild(sp1,1);
  4. auto sp2 = Sprite::create("CloseSelected.png");
  5. sp2->setPosition(Point(100,200));
  6. this->addChild(sp2,1);
复制代码
创建容器 
  1. Alpha:
  2. auto sp_array = Array::create();
  3. beta:
  4. Vector sp_vec;
复制代码
2、将创建好的精灵添加进容器中 
  1. Alpha:
  2. sp_array->addObject(sp1);
  3. sp_array->addObject(sp2);
  4. beta:
  5. sp_vec.pushBack(sp1);//和 堆栈 一样一样的
  6. sp_vec.pushBack(sp2);
复制代码
3、获得容器中的大小 
  1. Alpha:
  2. int count = sp_array->count();
  3. beta:
  4. int count = sp_vec.size();
复制代码
4、获得容器中的精灵,并让这些元素都做统一的动作 
  1. Alpha:
  2. for(int i=0;icount();i++)
  3. {
  4. auto sp = (Sprite*)sp_array->getObjectAtIndex(i);
  5. sp->runAction(MoveTo::create(0.2f,Point(100,100)));
  6. }
  7. beta:
  8. for( auto& e : sp_vec)
  9. {
  10. e->runAction(MoveTo::create(0.2f,Point(100,100)));//这种for写法是C++ 11的新特性
  11. }
复制代码
5、删除容器中的精灵removeLastObject(); 
  1. beta:
  2. sp_vec.popBack();
  3. //如果是直接删除对象:
  4. Alpha:
  5. sp_array->removeObject(sp1);
  6. beta:
  7. sp_vec.eraseObject(sp1);
  8. //如果是要删除容器中全部的对象:
  9. Alpha:
  10. sp_array->removeAllObject();
  11. beta:
  12. sp_vec.clear();
复制代码
6. struct timeval   :有两个成员,一个是秒,一个是微秒, 所以最高精确度是微秒。一般由函数int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统的时间。 



0 0
原创粉丝点击