C++ Question - Initializing Variables differently

We can initialize variables differently in C++ and Cocos. Can anyone explain them and which should we use. Should we use different ones for different reasons. Just general knowledge if you understand them well. So that i can understand them better. Thanks

EXAMPLE ONE - H

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include "cocos2d.h"

class TestClass
{
	public:

		TestClass(){};
		~TestClass(){};

	private:

		float m_Scale = 0.0f;
		cocos2d::Sprite* m_Cursor = cocos2d::Sprite::create("image.png");

};

#endif // TESTCLASS_H

EXAMPLE ONE - CPP

#include "TestClass.h"

`//NOTHING IN CPP`


EXAMPLE TWO - H

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include "cocos2d.h"

class TestClass
{
	public:

		TestClass();
		~TestClass(){};

	private:

		float m_Scale;
		cocos2d::Sprite* m_Cursor;

};

#endif // TESTCLASS_H

EXAMPLE ONE - CPP

#include "TestClass.h"

TestClass::TestClass(){
m_Scale= 0.0f;
m_Cursor = cocos2d::Sprite::create("image.png");
}


EXAMPLE THREE - H

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include "cocos2d.h"

class TestClass
{
	public:

		TestClass();
		~TestClass(){};

	private:

		float m_Scale;
		cocos2d::Sprite* m_Cursor;

};

#endif // TESTCLASS_H

EXAMPLE THREE - CPP

#include "TestClass.h"

TestClass::TestClass():
m_Scale(0.0f),
m_Cursor(cocos2d::Sprite::create("image.png"))//NOT SURE IF THIS ACTUALLY WORKS
{}

both are ok to work with… But for initiating a sprite I would recommend you have a class init() function and create the sprite there.

1 Like

TestSprite.h (465 Bytes)
TestSprite.cpp (476 Bytes)

I suggest Something like this. If inherited from node that is :slight_smile:

1 Like