Jump to content
  • Advertisement

Archived

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

bobbias

using struct in c++

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

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??

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!