Advertisement

using struct in c++

Started by August 26, 2002 07:57 PM
13 comments, last by bobbias 22 years, 4 months ago
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:
The Undead shall rule forever.
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
[Cyberdrek | ]
Advertisement
Hey dont worry about it, happens to me all the time. It was meant to be constructive criticism and i hope thats how you took it.

peace
Class defaults to private, not protected (struct defaults to public). Other than the default public/private struct & class are synonymous in C++.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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).

This topic is closed to new replies.

Advertisement