View Full Version : C++ Promblem
ProgrammingFreak
04-22-2006, 04:59 PM
I am having trouble understadning this programming concept
Example:
typedef struct{
int Helath;
int Armor;
}b_stuff;
Yes that is an example of it, I understand how to do that, but how do you give the variables vaules after they have declared in there, I tried it out of there and I got an error, I tried it in the array and got an error, any help
RoadMaster
04-22-2006, 05:27 PM
Edit: Removed because others have answered your question better, and I clearly need to re-read some of my own C++ books as I've forgotten the differences between C and C++, dur...
svero
04-22-2006, 09:13 PM
There's two concepts here.. the typedef and the struct
The typedef is just a way of assigning a new name to some type when it's more convenient or if you're having trouble withe the pre-processor (#define's don't work sometimes with pointer types)
So instead of... Sprite* you could write...
typedef Sprite* PSprite;
and then define a function that takes a PSprite (pointer to sprite)
--
Now the struct is something else.. That's a way of grouping things together that belong together. Here's a useful example...
struct PLAYER
{
int Lives;
int Level;
int points;
};
theres a basic struct that has all the information about a player in a basic arcade game. What level they're on, how many lives they have left, and how many points they've accumulated. Then somewhere else in the code you might do...
PLAYER player1;
PLAYER player2;
to create 2 actual players. Or alternately...
PLAYER Players[2];
and then in the game where the player gets some points...
Players[CurrPlayer].points +=250;
etc...
A small note is that a struct is equivalent to a class in C++ where everything is public by default.. so these are the same..
struct A
{
int x;
int y;
};
class A
{
public :
int x;
int y;
};
ggambett
04-22-2006, 09:20 PM
You can also init static struct data this way
static b_stuff lStuffs[] = { { 1, 2, 3 }, { 4, 5, 6 } };
This creates an array of two b_stuff structs, the first with 1 life, level 2 and 3 points, and the other with values 4, 5 and 6.
Or create a constructor (the only difference between structs and classes in C++ is that classes are private by default and structs are public).
b_stuff::b_stuff (int nLives, int nLevel, int nPoints) : nLives(nLives), nLevel(nLevel), nPoints(nPoints) {}
You can use it this way
b_stuff pStuff1(1, 2, 3);
or
b_stuff* pStuff2 = new b_sStuff(1, 2, 3);
mahlzeit
04-23-2006, 01:36 AM
Small note: There is a difference between C and C++ here. In C, you do need the typedef but in C++ you don't. (Although they may have lifted that restriction in a recent revision of the language specs: the last time I used pure .c files was ages ago.) Anyway, if you look at old sources that may be confusing a little.
ProgrammingFreak
04-23-2006, 09:17 AM
Ok I get it kinda
dmikesell
05-16-2006, 04:09 AM
You may either want to start with a simpler language (Python, Ruby, etc.) or consider a simpler application than a game with which to learn C++.
Powered by vBulletin™ Version 4.1.3 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.