How can I use CCSpriteBatchNode add a subclass of CCSprite

I create a subclass of CCSprite which named Bullet,

class Bullet : public CCSprite
{
public:
    static Bullet* bullet();
...
Bullet* Bullet::bullet()
{
    this->create("bullet.png");

    return this;
}

but when i use CCSpriteBatchNode add it. it will crash.

CCSpriteBatchNode* batch = CCSpriteBatchNode::create("bullet.png");
Bullet *bu = Bullet::bullet();
batch->addChild(bu);

I don’t know what’s wrong with me. maybe its need the same texture.
I don’t know how to initialize the Bullet will get same texture with CCSpriteBatchNode;

You can help me. Thanks.

nobody know, maybe somebody can tell me how to inherit CCSprite via C++.

nobody know, maybe somebody can tell me how to inherit CCSprite via C++.

Crash is not because of you adding your object to CCBatchNode.
In fact the crash lies in your sprite creation code.

this->create("bullet.png");

and also note that create is a super class (CCSprite) method which returns CCSprite. Unless you over ride to
return Bullet object it will return CCSprite pointer.
This line is the cause of the crash.
If you have read any cocos2dx forums or documentation, create is a static method.
Key word “this” is a pointer that points to current object. But in your case you haven’t new anything so this is garbage at the time of function call.
The “this” inside bullet() is NULL (or garbage) at the time of method call and you are trying to call create() on a pointer to object that doesn’t exist and thous the crash.

All “create” methods in cocos2dx works when called with class.
eg: CCSprite::create();
CCBatchNode::create();
CCParticleQuad::create();

the create function is never meant to be called on this or any object.
For that purpose (initializing) there are init methods.

you can change your bullet class like this

class Bullet : public CCSprite
{
public:
    static Bullet* bullet();
...

// basically, you are copying the same code that
// is there in the CCSprite::create() function
// but with Bullet class instead of CCSprite.
// +TIP:+ This function can be named as create to maintain
// consistency in naming convention :)

Bullet* Bullet::bullet()
{
    // creating a new Bullet sprite object.
    Bullet *mBullet = new Bullet();

    // 1: checking if the memory is allocated
    // 2: calling init and if init returns true then
    // sprite is initialized
    if( mBullet && mBullet->initwithFile("bullet.png") ){
        mBullet->autorelease();
        return mBullet;
    }

    // if sprite is not initialized delete and return NULL
    CC_SAFE_DELETE(mBullet);
    return NULL;
}