Thats like saying why do you have std::string 3 times and int 21 times.
The reason being is the one in your constructor prototype is the type of parameter that will be passed to the function.
The private variable is the one you will be using in the class
and the one in your constructor definition is the one being passed into your object.
The reason being is the one in your constructor prototype is the type of parameter that will be passed to the function.
The private variable is the one you will be using in the class
and the one in your constructor definition is the one being passed into your object.
Could you exlain that a little more in depth please.
The prototype tells the compiler what the function is supposed to look like so that in the future (meaning code that comes after the prototype but before the definition) the compiler will be able to tell that that function exists if it's referred to.
The private variable is the variable itself. It's the real pointer.
The one in the definition is the one referred to by the prototype, but it's also one that's being passed in to the function itself (by reference instead of by value) from another function, and so is useable inside of the constructor as a predefined variable. It is most likely either containing information to be put into the private variable or containing information obtained from the private variable. Without actually reading the function, I wouldn't be able to tell you, but based on the fact that it's a constructor, I would say that the first is the greatest likelihood.
its still a little vague but i think im getting it more, i'll have to read over your post a few more times to understand it better, but theres one thing i still dont get, why does the pointer hold the variable when you dont assign it anything? im used to doing this:
int i = 700;
int *p_int = &i;
cout << *p_int << endl;
also why dont i put a pointer on the plr_inventory in the list here
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Player::Player(int MONEY, std::string NAME, int EXPERIENCE,
int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
int DECRYPTIONTOOLPRICE, bool *PLR_INVENTORY
):
money(MONEY), name(NAME),
experience(EXPERIENCE),
accountsHacked(ACCOUNTSHACKED),
PasswordCrackerLvl(PASSWORDCRACKERLVL),
DecryptionToolLvl(DECRYPTIONTOOLLVL),
PasswordCrackerPrice(PASSWORDCRACKERPRICE),
DecryptionToolPrice(DECRYPTIONTOOLPRICE),
--> plr_inventory(PLR_INVENTORY)
remove the reference. but yes.
you only use the reference for a normal int and stuff
say you have two int * types. You can do a = b you don't do a = &b that is for int * = int not int * = int *
*edit well you could use the reference...but that would be something completely different that would give you the address of the other pointer that you are trying to set this one equal to...
Why only for a normal int and not for the class? so int *p_int = &somenumber;
would equal the memory address of it, so in a class int, int *plr_inventory just assigns the memory address and then automatically dereferences it? or it just stores the variable itself?
You don't want to have a pointer point to the memory address of a pointer you want the pointer to point to what the other pointer was pointing to. there fore set pointer1 = pointer2
ok so let me see if i understand this correctly, so in the class in the prototype, i create a pointer and that just lets the program know what type of parameter will be passed right? and the one in the constructor is the one that will be passed to this
Player pc(1200, "Default", 0, 0, 1, 1, 100, 150, plr_inv);
correct? and i create bool plr_inv[2] = {false, false}; which is passed into the object, but in what pointer is it stored? *PLR_INVENTORY or *plr_inventory?
what does the private one do again? is that the one that stores the boolean variables?
There are tons of uses for using pointers to pointers. In fact, an iterator of a vector is technically a pointer to a pointer. Let's take the most basic example possible, a dynamically allocated two-dimensional array:
The reason why the second one uses the address symbol and the first one doesn't is because the variable plr_inventory is a pointer, and so a direct assignment to that variable needs to be the address of another variable, but an indirect assignment (using the indirection operator *) needs to be the variable itself.
int Ero(0);
int* a = &Ero;
int* b = &a; //Illegal; b must point to an int and not int*
int** c = &a; //Coolio; c totally points to an int*
When thinking about what pointers point to, simply take away one asterisk.
Anyway...
ok so let me see if i understand this correctly,...
Prototypes:Player(int, string/*...*/);
You can safely ignore the variable names in a prototype. They don't really affect anything. You could have one name in the prototype and another in the definition, and there would be no problems. Heck, you could even leave the parameters nameless in the prototype. The important information is the type.
Definitions:
1 2 3
Player::Player(int hp, string name/*...*/)
: plr_inventory(PLR_INVENTORY) //Copy pointer to the member variable
{}
The variables in the constructor is what you pass to, not the other way around. They recieve the arguments you pass, which will then be used inside the function body, such as for initializing member variables.
Member variables:bool* plr_inventory
When you pass plr_inv to the constructor, it will be assigned to PLR_INVENTORY. Then, PLR_INVENTORY will be copied to plr_inventory. At the end of the function body, PLR_INVENTORY is destroyed. So, for a short time, the passed pointer is stored in both, but ultimately it should be stored in plr_inventory.
EDIT:
Actually, it's possible that I mixed that up. One of the things that I always never do is make an assignment to a pointer during declarations except for NULL. So I'm more likely to do what I showed as the second example than the first. I'm pretty out of practice with assigning pointers anything except for NULL and then making an assignment later.
ok so now im really confused with all of this can someone tell me in a concise way what is going on and what its doing with pointers. also can you sum up what all was said but please be really clear, i am on the verge of understanding this but its still a little muddy.
class Test
{
public:
Test( std::vector<bool> );
private:
std::vector<bool> inventory;
};
Test::Test( std::vector<bool> inv ) : inventory( inv ){} //These two are almost the same one initializes and the other assigns value
/***Test::Test( std::vector<bool> inv )
{
inventory = inv;
}***/
int main()
{
std::vector<bool> v_inv( 2 , false ); //initialize a vector size 2 with all to false or do the second
/*** std::vector<bool> v_inv;
v_inv.push_back( false ):
v_inv.push_back( false );***/
//initialize empty vector there are more methods..
Test t( v_inv );
}
//vs
class Test
{
public:
Test( bool * );
private:
bool *inventory;
};
Test::Test( bool *inv ) : inventory( inv ){}
int main()
{
bool *p_inv = newbool[2]{ false , false };
Test t( p_inv );
delete[] p_inv;
}
I would honestly pass by a const reference though since you are not actually modifying the stuff you are just copying it to the private variable. No need to create a second useless copy before leaving the local scope of the constructor but I was just trying to make it as simple as possible.
I think the best advice I can give you right now is to look through the reference section of this website, and read each function in your code one at a time until you understand what it does, using the reference section as a........reference.
Because honestly, if you're not sure what it's even supposed to do, then there's no way you'll ever improve it.
ok ill try it but im telling you that is even more difficult for me to do but i will try, please give me the link and i'll look everything over and come back with anything i dont understand