Search code examples
cocos2d-x

why class can call the no static function?


bool GameOverLayer::init()
{
    if (CCLayerColor::initWithColor(ccc4(255, 255, 255, 255))) {
        return true;
    }
    else
    {
        return false;
    }
}

the function initWithColor isnot static function , why i can called it with cclayercolor?

define initWithColor function as below code :
bool CCLayerColor::initWithColor(const ccColor4B& color)
{
   CCSize s = CCDirector::sharedDirector()->getWinSize();
   this->initWithColor(color, s.width, s.height);
   return true;
}

Solution

  • GameOverLayer is inherited from CCLayerColor and initWithColor function is public and non-static so in code you can use this statement:

    CCLayerColor::initWithColor(ccc4(255,255,255,255));
    

    that means call inherited function from selected parent.
    if you don't like this type of calling you can use:

    this->initWithColor(ccc4(255,255,255,255));
    

    if you like to know more about this type of programming read more about inheritance and multiple inheritance. you can start in here and here