• Advertisement
Sign in to follow this  

Global scope

This topic is 4223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I use global scopes? I want to use functions from one class in another class. I am creating my Battle class, but it needs access to Player. Can I allow Battle class to use Player functions without deriving it? Scopes always confuse me. Can someone explain static, virtual, and Constructors/Destructors initialize variables and delete variables, correct? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
tatic, virtual, and Constructors/Destructors: these are all very different things, and I don't think you want any of them in this case.

I think what you really want to do is pass in your player object as a parameter to a method of your Battle class:

void Battle::doStuff(Player ply)
{
ply.funcInsidePlayer();
}


If not we can talk more about static methods (which is most likely what you would need).

Matt

Share this post


Link to post
Share on other sites
Yea, those things aren't import at the moment. They were just a few things that were annoying me because I don't know what they are used for or how they are used.

I just tried what you said, and yes that is what I want, but it gives errors.

Here are the errors:

battle.cpp:18: error: variable or field `LevelUp' declared void
battle.cpp:18: error: `int cBattle::LevelUp' is not a static member of `class cBattle'
battle.cpp:18: error: `cPlayer' was not declared in this scope
battle.cpp:19: error: expected `,' or `;' before '{' token

and heres the code for the player.h and the battle class

Player.h
#ifndef CPLAYER_CLASS
#define CPLAYER_CLASS
//--------------------------------------------------
// Include Files
//--------------------------------------------------
#include <windows.h>
#include <string>

//--------------------------------------------------
// Player.h class
//--------------------------------------------------


class cPlayer
{
private:

std::string P_Name;

int P_Powerlevel;
int P_MaxPowerlevel;
int P_Attack;
int P_MaxAttack;
int P_Defense;
int P_MaxDefense;
int P_Ki;
int P_MaxKi;
int P_Level;
int P_Experience;
int P_MaxExperience;
int P_Zenni;

public:

std::string GetName();

int GetPowerlevel();
int GetMaxPowerlevel();
int GetAttack();
int GetMaxAttack();
int GetDefense();
int GetMaxDefense();
int GetKi();
int GetMaxKi();
int GetLevel();
int GetExperience();
int GetMaxExperience();
int GetZenni();

void SetName(std::string newname);
void SetPowerlevel(int newpowerlvl);
void SetMaxPowerlevel(int newmaxpowerlvl);
void SetAttack(int newattack);
void SetMaxAttack(int newmaxattack);
void SetDefense(int newdefense);
void SetMaxDefense(int newmaxdefense);
void SetKi(int newki);
void SetMaxKi(int newmaxki);
void SetLevel(int newlevel);
void SetExperience(int newexperience);
void SetMaxExperience(int newmaxexperience);
void SetZenni(int newzenni);

void ChangePowerlevel(int amount);
void ChangeMaxPowerlevel(int amount);
void ChangeAttack(int amount);
void ChangeMaxAttack(int amount);
void ChangeDefense(int amount);
void ChangeMaxDefense(int amount);
void ChangeKi(int amount);
void ChangeMaxKi(int amount);
void ChangeLevel(int amount);
void ChangeExperience(int amount);
void ChangeMaxExperience(int amount);
void ChangeZenni(int amount);
//To lower values, use changew/e and just use a negative number
};
#endif



Battle.h

#include "battle.h"

cBattle::cBattle()
{
//ctor
}

cBattle::~cBattle()
{
//dtor
}

void cBattle::DeathCheck()
{

}

void cBattle::LevelUp(cPlayer player)
{
if (cPlayer.GetExperience >= cPlayer.GetMaxExperience)
cPlayer.SetLevel(cPlayer.GetLevel + 1);

}




Guess I do need to know about static members. :-

Share this post


Link to post
Share on other sites
Ignore everything. I fixed it. I forgot to include the class I wa susing, and I missed closing a bracket for the function. Thanks for your help.

Share this post


Link to post
Share on other sites
This function is broken. It doesn't do what you think it does.

void cBattle::LevelUp(cPlayer player)
{
if (cPlayer.GetExperience >= cPlayer.GetMaxExperience)
cPlayer.SetLevel(cPlayer.GetLevel + 1);
}
I'll give you a hint. This function has the same problem:

void SetToZero( int x )
{
x = 0;
}

Share this post


Link to post
Share on other sites
Huh? What's wrong with the SetToZero function you have there??? It looks fine to me. Your getting an integer which you then set to 0. How is it wrong? Unless, your using the wrong variable? Actually yea. int blah SetToZero(blah) it would make blah go into x but not change the value of blah o.O Am I right?

For my function though, can you help me out a little more because I've never done anything like this (cPlayer player) I want to make the function able to take any object made from the player class and be able to change all of it's statistics.

Hope you understood all that lol. Thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the problem with setZero is that you are passing the variable by value.
so:


void SetToZero( int x ) {
x = 0;
}



will create a new variable named 'x' that is only used within the function that has the same value as whatever you pass to it.

so someobject.SetToZero(myCoolVariable);
would do exactly the same as someobject.SetToZero(5);
(if myCoolVariable contains the value 5)

in order to allow SetToZero to modify the variable you pass to it you need to pass the address of the variable,

either by using

void SetToZero(int *x) {
(*x)=0;
}
somecrap.SetToZero(&somevariable)



or the better and easier option

void SetToZero (int &x) {
x=0;
}
somecrap.SetToZero(somevariable);

Share this post


Link to post
Share on other sites
Ohhh. I haven't used this before '&'. I knew it had to do with addresses, but didn't know much about. Pointers are another thing that confuse me. I think this should work.


void cBattle::LevelUp(cPlayer &player)
{
if (player.GetExperience() >= player.GetMaxExperience())
player.ChangeLevel(1);
player.ChangeMaxAttack(2);
player.ChangeMaxDefense(2);
player.ChangeMaxExperience(200);
player.ChangeMaxKi(5);
player.ChangeMaxPowerlevel(10);
player.ChangeZenni(3);
}



Where into the function, all those player.blah are now w/e player has the address of. So now it can work on say Chicken, Dog, Cat and still not bother the other animals so to speak?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by SonicD007
Ohhh. I haven't used this before '&'. I knew it had to do with addresses, but didn't know much about. Pointers are another thing that confuse me. I think this should work.

*** Source Snippet Removed ***

Where into the function, all those player.blah are now w/e player has the address of. So now it can work on say Chicken, Dog, Cat and still not bother the other animals so to speak?


yup pretty much, (if Chicken, Dog, Cat, etc are players that is)

basically a pointer contains a memory address

so
int a = 5;
int *b = &a;

would create something like this. (crappy ascii picture)
--- ---
|b| ----> |a|
|*| |5|
--- ---

a is a variable with the value 5,
b is a pointer that "points" at a. (b:s value is the address of a, or &a)

given an address you can access the value stored by using a dereference.
(*b) is 5.

Share this post


Link to post
Share on other sites
Quote:
Original post by SonicD007
So now it can work on say Chicken, Dog, Cat and still not bother the other animals so to speak?

No, presuming that by referring to Chicken, Dog and Cat you are referencing the common (and stupid) inheritance examples of creating a base class named Animal and derived classes as previously mentioned.

A reference, as created using the address-of operator (&) in a parameter list, contains the address of an object of that type, and of that type only. To enable polymorphism (the ability to treat an object of one type as another, related type), you must use pointers in C++.

The Anonymous Poster above conflates a pair of uses of the address-of operator:
  1. It can be used to obtain the address of an existing variable, as in the example:
    int a = 5;
    int * addr = &a; // addr contains the address of a
    *addr = 4; // we _dereference_ addr to access and modify the value
    cout << a << endl; // prints 4, because that's the new value of a


  2. It can be used to create a reference variable:
    int a = 5;
    int & b = a; // note the placement of the &, and the absence of *
    b = 4;
    cout << a << endl; // prints 4, as b is effectively an alias for a
    int c = 6;
    b = c; // has the effect of setting the value of a to the value of c
    cout << a << endl; // prints 6

The use of references is the second example, allowing you to pass an object to a function and obtain an alias that modifies the original object. C++ however, thanks to its C legacy, is a little confusing, so there's one more use of the & symbol that is completely unrelated to the above:
int a = 5;         // in binary, this is 00000101
int b = 12; // binary: 00001100
int c = a & b; // this performs a bitwise AND. result: 00000100
cout << c << endl; // prints 4

There's also &&, which is a logical AND, similar to the bitwise AND above but operating on boolean variables.

Share this post


Link to post
Share on other sites
Ok, so I have just one class. The player class. Nothing derived from it at the moment. If I create just Cat,Dog,Chicken from the player class, then the function that AP helped me with would work wouldn't it? If they were NOT of that class and were mixed up, then that's when I would need to use pointers and references?


The last thing you showd me, the bitwise and all that binary stuff, will I be needing to do stuff like that anytime soon in game programming because it looks really confusing @.@ The && operator I know. Just all that binary stuff you had and how exactly does c = 4 using the bitwise operator? (never seen stuff like that before, so this is new for me)

Share this post


Link to post
Share on other sites
Quote:
Original post by SonicD007
Ok, so I have just one class. The player class. Nothing derived from it at the moment. If I create just Cat,Dog,Chicken from the player class, then the function that AP helped me with would work wouldn't it? If they were NOT of that class and were mixed up, then that's when I would need to use pointers and references?

You would still need references if you wanted to modify these instances of Player inside your function. Otherwise the function would receive copies of the objects, not aliases. Before the introduction of references in C++, you would have needed pointers, which would have uglied up your code somewhat (->, the pointer-to-member operator, instead of ., the member access operator, being the main culprit). (Hogwash. See rip-off's post below.)

Quote:
The last thing you showd me, the bitwise and all that binary stuff, will I be needing to do stuff like that anytime soon in game programming because it looks really confusing @.@ The && operator I know. Just all that binary stuff you had and how exactly does c = 4 using the bitwise operator? (never seen stuff like that before, so this is new for me)

You won't need binary math just yet (or necessarily ever), but it's very useful to know. Binary is base-2; our common number system, decimal, is base-10. To convert a decimal number to binary, you keep dividing the number by 2 and writing out the remainder until you have a zero. So, to convert 143 from decimal to binary:
2|143|R
-------
| 71|1
| 35|1
| 17|1
| 8|1
| 4|0
| 2|0
| 1|0
| 0|1

Reading the remainder column upwards, 14310 == 100011112.

To convert a binary number to decimal, you multiply the number in each digit place by 2 raised to the power of one less than that digit place and sum up the values:
10001111 = 1x27 + 0x26 + 0x25 + 0x24 + 1x23 + 1x22 + 1x21 + 1x20
= 128 + 0 + 0 + 0 + 8 + 4 + 2 + 1
= 14310

When performing bitwise operations, each binary digit or bit from each of the operands is operated on independently:
    00000101
AND 00001100
------------
00000100

AND (&) returns 1 where both bits are 1 in a column. OR (|) returns 1 where either bit is 1 in a column. XOR (^) returns 1 where one and only one of the bits is 1 in the column. The NOT operator, ~, inverts each bit (returns a 1 where there was a zero, and vice versa), but only operates on one operand: ~00000101 = 11111010

That's bitwise operations in a nutshell.

[Edited by - Oluseyi on July 28, 2006 10:26:57 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
A reference, as created using the address-of operator (&) in a parameter list, contains the address of an object of that type, and of that type only. To enable polymorphism (the ability to treat an object of one type as another, related type), you must use pointers in C++.


No? (feels wrong saying no to you [smile])

References also enable polymorphism:

class Base
{
public:
virtual void print() { std::cout << "Hello world\n"; }
};

class Derived : public Base
{
virtual void print() { std::cout << "Goodbye world\n"; } // subtle change
};

void callPrint( Base &base )
{
base.print();
}

int main( int argc, char **argv )
{
Derived derived;
callPrint( derived );

Base base;
callPrint( base );
}


Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
No? (feels wrong saying no to you [smile])

It shouldn't. I'm not infallible. [smile]

That's a pretty big, gaping hole in my understanding of references. I suppose that some erroneous document I read long ago (10 years... was it always this way? Curious.) continues to influence me to this day, underscoring the importance of providing beginners with accurate information.

Thanks for the catch! (rate++)

Now I have to go trawl my code archives to see where I need to refactor in light of this...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement