Jump to content
  • Advertisement
Sign in to follow this  
SonicD007

Global scope

This topic is 4525 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

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
Advertisement
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!