What you do:
What you must do:Code:charater* p1; p->whatever();
This boils down to how pointers work.Code:// Either character* p1 = new character; p1->whatever(); delete p1: // Or character p1; character* pp1 = &p1; pp1->whatever();
The first example creates no character whatsoever and tries to access a non-existent character.
The second example shows two ways of creating a character and setting the pointer to point to it.
As for the class, I'll demonstrate a function:
References are also possible:Code:void foo(character* p) { p->whatever(); } int main() { character p1; foo(&p1); }
Foo needs to take the player it needs to work on as an argument.Code:void foo(character& p) { p.whatever(); } int main() { character p1; foo(p1); }
Remember: classes are ONLY blueprints. Like a car blueprint.
A car does not exist until it's built from a blueprint. And there can be several cars from the same blueprint.
The same applies to characters.
This is the basic understanding I want you to understand, which is why I said go back to studying pointers.



LinkBack URL
About LinkBacks





