How to stop money from going over

Pages: 1... 4567
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.

That's the same for your ints and strings.

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
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)
Last edited on
because plr_inventory changed the address it is pointing to the address of PLR_INVENTORY

1
2
3
4
int i = 700;
int *p_int = &i;
int *p_int2 = p_int; //They are now pointing to the same place( the address of i ).
std::cout << *p_int2 << std::endl;


http://www.cplusplus.com/doc/tutorial/pointers/
http://www.learncpp.com/cpp-tutorial/67-introduction-to-pointers/
http://www.learncpp.com/cpp-tutorial/68-pointers-arrays-and-pointer-arithmetic/
ok, but where does that change occur?

--> plr_inventory(PLR_INVENTORY)?
Last edited on
yes.
ok so basically this: --> plr_inventory(PLR_INVENTORY)

is the same as this?

bool PLR_INVENTORY;

bool *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...
Last edited on
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?
-1 for this last exchange.

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
int ** arr = NULL;
if(!(arr = malloc(sizeof(int *) * x))){
        mem_clean(arr);
        return ERRNOMEM;
}

int i = 0;
while(*(arr + i)){
        if(!(*(arr + i) = malloc(sizeof(int) * y))){
                mem_clean(arr);
                return ERRNOMEM;
        }
}


As you can see above, you absolutely do want (sometimes) to have pointers pointing to other pointers. It wouldn't be possible to do this otherwise.

That being said, giblit is right about this case of pointer. It should be equivalent to:
1
2
bool PLR_INVENTORY;
bool * plr_inventory = PLR_INVENTORY;


OR:
1
2
3
bool * plr_inventory = NULL;
bool PLR_INVENTORY;
plr_inventory = &PLR_INVENTORY;

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.
*edit well you could use the reference...

Not really.
1
2
3
4
5
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.

Hope this helps.


Edit:
@ciphermagi
ciphermagi wrote:
1
2
bool PLR_INVENTORY;
bool * plr_inventory = PLR_INVENTORY;

Why? :(
Last edited on
Indirection

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.
Last edited on
But according to that code, plr_inventory will be pointing to either 0x000 or 0x001 depending if PLR_INVENTORY is true or false.

Glad we cleared that up.
Last edited on
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.
Last edited on
Okay ch1156..

I think it's safe to say you need to read up on
functions and pointers.

For now how about you use a std::array , std::vector , or another stl container.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 = new bool[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.
Last edited on
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
Pages: 1... 4567