0
I need an overview on what the pointer, array, and constructors do in a/ for a game.
What array, pointer, and contructors do.
4 Antworten
+ 2
array: for storing game objects of a particular type. for eg an array of enemies. it keeps game objects of different types separated.
constructor: for initialising the game objects. example would be giving random x and y coords or type(goblin,bat etc) to the enemy.
pointer/reference: for modifying the gameobjects in a different funcation. eg to move the enemy in a function called move. if the obj is passed without pointer/reference, then a copy of the obj would be created and the move function would be called on that copy. we wont see the enemy move. references are easier to use.
void move(GameObject& gObj){
gObj.x++;
}
move(goblin);
+ 2
&var_name returns the memory.
type& var_name=var_a;
is a simpler way of referencing a variable. its like pointers but it needs less code and cant be reassigned to point to a different variable. it has to be initialised at the time of declaration.
type* ptr;
can point to any variable with type 'type'. it doesnt have be to initialised at the time of declaration can can be reassigned to point to other var. it stores the memory address of the variable. eg.
ptr=&a;
ptr=&b; // changed to point to address of b
for accessing data from a memory address the asterisk is used followed by the memory address.
int num=42;
cout << "address" << &num << endl;
cout << "data at address" << *(&num) << endl; // prints 42
since a pointer holds a memory address. you can retrieve the data at the address by
cout << *ptr << endl;
you can also change the data at the address by doing this
*ptr=56;
keep in mind that the ptr is pointing to the address of a variable in memory. therefore changing the memory changes the...
+ 2
...variable's value too.
reference operator (&) privides a simpler way of doing all this. for eg you can just do this without caring about the asterisks.
int x=34;
int& ref=x;
ref now holds a reference of the variable x
you can now do something like to change its (x's) value
ref=53;
cout << ref; // 53
0
Farry, what is the difference between * and & when using pointers.