Hey guys not really a problem but another one of my is this right questions. I’m working on a space shooter (a lame one for now). I’m trying to clean up my code by passing an array of classes. When I do this, is there a need to pass it by reference or some other form of working with the direct memory address of the class? Because I wrote some test code but when I tried to pass it as a reference I got errors but when I just passed the array itself it seemed to have modified the data. I just want to make sure this is the right way before I implement it in my stupid game
Here the code for my test class that seems to do what I want without references.
Code:
#include <iostream>
class test
{
public:
void setX(int x){xPos = x;}
int printX(){return xPos;}
private:
int xPos;
};
void function(test move[],int numOfElms);
int main(void)
{
test brian[5];
function(brian,5);
for(int i = 0; i < 5; i++)
{
std::cout<<brian[i].printX();
}
std::cin.get();
return 0;
}
void function(test move[],int numOfElms)
{
for(int i = 0; i < numOfElms; i++)
{
move[i].setX(i);
}
}
Output: 0,1,2,3,4
So it looks to me as its modifying the values in brian not just making a copy of it