Archived

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

using struct in c++

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

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
lol, thank you.
I got confused between classes and structs and forgot that classes are by default private.
Anyway, i was writing psuedo code and i didnt really expect him to copy what i wrote, just to get the idea, so i was more lenient with what i wrote.
dont forget to put public:

Share this post


Link to post
Share on other sites
quote:
Original post by battering ram
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]


In a way you''re right except for one part. The C++ definition of a struct is that a struct is a class which defaults to public. In other words, they are pretty much the same in C++.


"DaHjajmajQa''jajHeghmeH!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In C++, a struct *is* a exactly the same as a class except for 2 propriets:

(1) members of a struct default to being public, whereas members of a class default to being private

(2) a struct with only data is considered to be a POD structure (plain old data, and that *is* the technical name for it). The C++ standard allows bitwise copying of POD structures. If you want further details, read http://gcc.gnu.org/ml/libstdc++/2002-04/msg00298.html (it towards very bottom).

Share this post


Link to post
Share on other sites