Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Xeneth

Switch statement in classes

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

This is in console, in c++. Can I setup a switch in a class and then in the object setup the case''s? What Im doing is seting up an 2D array (array[X][Y])of area classes. in the class I whant to setup a switch statement for ''n,s,e,w''. Then each object will setup what the cases do(n ''x += 1'', s ''x -= 1, w ''y-=1'', e ''y += 1) or if they can''t go that way print "unable to move that derection". Question is, how do I set the case in the object rather thin in the class? Hope that''s not to confusing to read. Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.

Share this post


Link to post
Share on other sites
Advertisement
Your Area class could have 4 bool members: n, s, e, w. If n is true then you can go north, etc.
The Area constructor could then be something like Area(bool n, bool s, bool e, bool w).
If you want to create a tile which allows only to go north and west you could do Area(true, false, false, true).
Then you could have methods like GoNorth() which would do x+=1 if n is true or print the error string otherwise.

Share this post


Link to post
Share on other sites
The language specifies that the cases are constant.

You can build an array of value->member function pointer look-up entries in your subclass if you want.

Microsoft technology solves problems that most other technology doesn''t; Microsoft business practices remind me of... better not say.

Share this post


Link to post
Share on other sites
quote:
Original post by Blew
Your Area class could have 4 bool members: n, s, e, w. If n is true then you can go north, etc.


Not a bad Idea, I can just put a, ifelse statement that duz all the calculations if true in each case.

Thanx




Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.

Share this post


Link to post
Share on other sites
You can use both solutions: boolean values and constants
Declare a char with the following values:
NORTH 1
EAST 2
SOUTH 4
WEST 8
Your flags are a byte within a char.

thus you can setup with:
switch (dir)
case NORTH:
case EAST:
case SOUTH:
case WEST:

And later, you can test wether a tile blocks or not the character:
if (Tile.dir && Character.dir)
... move
else
... block movement

Hope that helps.
Ghostly yours,
Red.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!