I am confused about something. I have the following example:


class mainMenu : public Node {
    static Scene* createScene();
    virtual bool init();
    void GoToGameScene(Ref *pSender);

    mainMenu() {};
    ~mainMenu() {};



Scene* mainScene::createScene(){
    auto scene = Scene::create();
    auto layer = mainScene::create();

    return scene;

Focus on this part.

auto scene = Scene::create();
auto layer = mainScene::create();

Why do we create layer from mainScene not from Layer

For example: Why we do this:

auto layer = mainScene::create();

If we can do it, like creating a scene

auto layer = Layer::create();

1 Answer 1


First of all, it's very uncommon to start classnames with a lowercase letter. It makes the code harder to understand.

Second, I'm not sure if I got your question right, but it seems like you're asking: Why call a create() method on the mainScene class instead of just using a simple Layer::create()?

If that's the case then the answer is quite simply: Because in this way we can customize how the mainScene should be initialized. Perhaps it needs several layers, like a HUD layer, a game layer, a controls layer, etc. By calling its create() method we make sure the constructor of mainScene is called, but also it's created as a Ref object, meaning that it'll be reference counted and therefore the memory management will be done automagically (for the most part).

If that wasn't the question feel free to explain it to me a bit further.

  • \$\begingroup\$ C++ Has no common conventions. He can write however he wants. This is also the main reason why C++ is such a terrible language. \$\endgroup\$
    – Krythic
    Commented Mar 3, 2017 at 3:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .