For passing around pointers you probably find better tutorials. But I can try to give you a very quick introduction using your EMissile class. When you create class objects you have usually 2 ways to do that:
Code: Select all
EMissile myMissile; // stack
EMissile * myOtherMissile = new EMissile(); // heap
The second one is on the heap. You can return it simply by returning myOtherMissile as it already is a pointer. Creating it is slow because new is more complicated function as it has to figure out stuff like where to get it's memory from. And you have to release the memory of that variable yourself at the end because it's memory is not released automatically. So you get memory-leaks if you never release that memory. The memory will stay valid until you call delete. Or for Irrlicht objects you call drop() which internally will call delete when it was the last reference to that object (Irrlicht does reference counting - but that's another topic). General rule to make it easier to work with such classes is - whoever created the object should be responsible for destroying it again (NOTE that Irrlicht does not always do that - so it is a bad architecture example - even good programmers mess that up once in a while).
So let's make a very short example of how to use functions with pointers:
Code: Select all
class Application
{
public:
Application()
: myOtherMissile(0), theThirdMissible(0) // always initialize all pointers to 0 to make your life a lot(!) easier
{
myOtherMissile = new EMissile(); // allocate heap memory
}
~Application()
{
delete myOtherMissile;
}
EMissile * getMyMissile() { return &myMissile; }
EMissile * getMyOtherMissile() { return myOtherMissile; }
void setThirdMissile(EMissile * missile) { theThirdMissible = missile; }
EMissile * getThirdMissile() { return theThirdMissible; }
private:
EMissile myMissile;
EMissile * myOtherMissile;
EMissile * theThirdMissible;
};
void main()
{
Application myApp;
EMissile * missileHeap = new Missile();
EMissile missileStack;
myApp.setThirdMissile(missileHealp);
myApp.setThirdMissile(&missileStack);
EMissile * missile = myApp.getMyMissile(); // would also work with other getters.
missile->Shield(); // call some function
}
About examples - they are kept as simple as possible because it's about showing how to do stuff with Irrlicht. They are not about teaching c++. You could use headers and certainly should when writing own applications. Check the Demo code - that works with headers.