Why the layer must be created from my class and not from layer class

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

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

private:

};


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();


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.

• C++ Has no common conventions. He can write however he wants. This is also the main reason why C++ is such a terrible language. – Krythic Mar 3 '17 at 3:27