using struct in c++

Started by
13 comments, last by bobbias 21 years, 8 months ago
I am going to use struct''s in my game and am wondering how to input into the struct form my main function? ould you help? anyone??
Advertisement
here is some psuedo code

struct foo_t {    float x,y,z;}foo_t foo;int main(){   foo.x = 10;   foo.y = 5;   foo.z = 2;   return 0;}  


i dont know if this is what your looking for, but if it isnt, i need some more detail on what you want.

The Undead shall rule forever.

[edited by - battering ram on August 26, 2002 9:10:23 PM]
The Undead shall rule forever.
thanks. uhh... is that c or c++?
What''s the difference? The code works in either.
the only difference(that im aware of) in structs between C++ and C is that C++ allows structures to have member functions, anyone that knows that im wrong please school me, ignorance isnt always bliss
lol thanks.
lol thanks. but i mean the . operator... I heard you could use the :: operator...
You really should get a book or find some tutorials before you try to make a game. I think you''ll have a lot of problems if you don''t understand the language, and structs are one of the most basic parts. There are a lot of great books out there; you should read one.
C++ gave structs more power, the ability to have constructors, destructors, and i think it can encapsulate functions, but if you want all of that in a struct, you'd be better off with a class.

the . operator is used even with C++ classes.

class Foo {   float x,y,z;}Foo fool;int main(){    fool.x = 10;return 0;}   


However, the -> operator is used with structs and classes when the instance is a POINTER to the struct/class.

Foo *fool;fool->x = 10;  


EDIT: i forgot to explain ::

The :: operator is used in defining a function inside a class.
There are two ways to define a function in a class
1st:

class Sprite {
void Draw() {};
}

2nd:

class Sprite {
void Draw();
}

Sprite::Draw() {
DoSomething()
}

The :: is used to define functions externally from the class in a cpp file, while the class and the declaration are inside the .h file
The Undead shall rule forever.

[edited by - battering ram on August 26, 2002 10:30:02 PM]

[edited by - battering ram on August 26, 2002 10:40:58 PM]
The Undead shall rule forever.
uhhhh.... batteringram you declared your class wrong... it should be...

class Foo{
public:
float x,y,z;
};

Foo fool;

int main()
{
fool.x = 10;
return 0;
}

note that the differences are a semicolon at the end of class and the declaration that float x, y, z are public and not protected which is the class default.

This topic is closed to new replies.

Advertisement