|
From: Jason H. <jas...@bt...> - 2002-03-24 00:33:04
|
The struct tag is a remnant of C carried through and extended in C++. =
Structs in C carry data only and don't allow methods and such to added, =
but a struct in C++ is equivalent to a class but with it's member =
automatically declared public by default, whereas in a class the default =
is private.
The only reason I can think of for using it in C++ is if it lends a =
clearer meaning to the code you are writing and the way it is =
represented, and possibly in compatibility with C, i.e. porting old C =
code to C++. Another difference between the C struct and the C++ struct =
is that in C a struct is a tag for a set of data formatted in a specific =
manner, and in C++ a struct represents a type. Mainly meaning that in C =
each declaration of a struct required you to specify that it was a =
struct, i.e.
struct a_structure data; // creates an a_structure struct in data.
In C++ this is still valid but you could also say:
a_structure data; // same
"a_structure" can be any structure previously defined. A work around you =
will see often in things like Win32 API is the use of a typedef for a =
struct, i.e.:
typedef struct tagPOINT { // pt=20
LONG x;=20
LONG y;=20
} POINT;
Where POINT is a typedef of the tagPOINT struct, and means you can =
declare a POINT directly.
An array is a primitive data representation used in C and C++ as the =
basic structured container available. An array of 1000 bytes directly =
represents 1000 consecutive memory locations in the underlying system. =
AFAIK the underlying code is supported by direct assembly =
representations of arrays (CISC), but don't quote me on that. One of the =
drawbacks of the basic array is that it has no bounds checking built in =
to it, so you could create an array of 1000 bytes and then go on to =
access 2000 bytes (and most likely crash your system). These errors can =
often be hard to find and require an extra investment in checking of =
bounds in your code.
On the other hand the Vector class (std::vector<type>) is a C++ =
container class that mimics much of what an array is, and most likely =
(although not guaranteed) is internally represented as an array or group =
of arrays. On top of that the class supports automatic resizing and =
bounds checking and a whole host of other manipulation methods and =
algorithmic functions. This extra functionality will incur an extra =
overhead on a system, but in modern C++ standard library implementations =
this cost will be negligible, and unlikely to be significant. As a C++ =
coder more often than not you would be recommended to use a vector =
although basic arrays will still have their uses occasionally.
Maybe this is more information that you wanted to know, but it may be =
valuable in future use.
Jason.
----- Original Message -----=20
From: Daniel Glenfield=20
To: dev...@li...=20
Sent: Saturday, March 23, 2002 8:43 PM
Subject: [Dev-C++] Struct tag
Hi, just been going over my course notes and I've come across the =
Struct tag. An example is below.
Looking at it, how is this different to a class? And why use it?
Also, what's the difference between an array, and a vector, which I =
understand is its replacement but know little about them.
// A demonstration of the Employee structure
#include <iostream.h>
struct Employee // declaration of a structure type
{
int EmpID;
char EmpName[30];
char EmpAddr[40];
float Salary;
void GetData();
}EmpOne; // Optional declaration of a structure variable
void main()
{
}
-------------------------------------------------------------------------=
-----
Join the world's largest e-mail service with MSN Hotmail. Click Here
_______________________________________________ Dev-cpp-users mailing =
list Dev...@li... =
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|