I've read explanations but to no avail. Could any one offer some explanation of what the difference is and what the benefits are of typedef, struct and classes?

struct and class are essentially the same thing, just with different default access. structs are public by default and classes are private.

typedef is a completely different beast, and I'm curious what kind of misunderstanding prompted you to lump them together. Perhaps this little trick from C to avoid typing the struct keyword with every instantiation?

typedef struct foo {
    /* ... */
} foo;

struct foo obj1; / * Okay, normal in C */
foo obj2; /* Use the typedef to avoid typing 'struct' */

As Narue explained, a struct and a class are essentially the same. The only real difference is that members of a struct default to the public access level while members of a class default to the private access level, unless otherwise specified in the declaration of the struct/class.

In some programming languages, you can create an "alias" for certain things. In C/C++, a typedef is used to create an "alias" for a previously-defined dataType, whether built-in (int, double, etc.) or custom-defined (via a class/struct/etc.).

For example, let's say your program uses unsigned short as the dataType for several critical pieces of data. It'll get tedious to repeatedly type "unsigned short" every time you need to create one; plus, as you use it more the likelihood of a typo increases. To simplify things, and to reduce the chance for an error, you can use a typedef: typedef unsigned short ushort; . Now, whenever you need that dataType, you can just use "ushort" instead.

As Narue explained, a struct and a class are essentially the same. The only real difference is that members of a struct default to the public access level while members of a class default to the private access level, unless otherwise specified in the declaration of the struct/class.

In some programming languages, you can create an "alias" for certain things. In C/C++, a typedef is used to create an "alias" for a previously-defined dataType, whether built-in (int, double, etc.) or custom-defined (via a class/struct/etc.).

For example, let's say your program uses unsigned short as the dataType for several critical pieces of data. It'll get tedious to repeatedly type "unsigned short" every time you need to create one; plus, as you use it more the likelihood of a typo increases. To simplify things, and to reduce the chance for an error, you can use a typedef: typedef unsigned short ushort; . Now, whenever you need that dataType, you can just use "ushort" instead.

Okay, not being lewd here, but that sounds pretty...um....pointless.

Okay, not being lewd here

You might want to look up the definition of lewd.

that sounds pretty...um....pointless.

You didn't specify which of the two things you consider pointless. For the similarities between struct and class, that's an understandable opinion, but there are good historical reasons for doing things that way.

For typedef, yes, it does seem pointless when you first learn it. But so do pointers. However, when you start working with complex types, or portability starts becoming an issue, the ability to define a type-safe alias is mighty handy.

You might want to look up the definition of lewd.


You didn't specify which of the two things you consider pointless. For the similarities between struct and class, that's an understandable opinion, but there are good historical reasons for doing things that way.

For typedef, yes, it does seem pointless when you first learn it. But so do pointers. However, when you start working with complex types, or portability starts becoming an issue, the ability to define a type-safe alias is mighty handy.

I know I have a terrible definition of some words, but what I understood from what the other poster said about how they define a dataType that came off to me as dumb because they only mentioned that it'd shorten the name referring to it for some reason, as a type.

I don't clearly get that, but if shortening a name is the only use then I don't really see how it's worth use.

And as dumb as this sounds, what do you people mean by "types" and "portability?"

"Shortening" probably isn't as good of a description as "simplifying", "changing" or "replacing". It's easier to remember a simpler name than it is a complicated one:

class SomeSampleClassThatIUseAllOverThePlaceInMyProgram {
};

typedef SomeSampleClassThatIUseAllOverThePlaceInMyProgram primaryDataType;

Based on this typedef, I can use "primaryDataType" instead of "SomeSampleClassThatIUseAllOverThePlaceInMyProgram".

A typedef also makes it much easier to change large portions of your program if necessary:

//example code 1
//this program declares 10 vectors of int
#include <vector>
#include <iostream>

typedef int vectorType;

int main() {
  vector<vectorType> myVector1;
  vector<vectorType> myVector2;
  vector<vectorType> myVector3;
  vector<vectorType> myVector4;
  vector<vectorType> myVector5;
  vector<vectorType> myVector6;
  vector<vectorType> myVector7;
  vector<vectorType> myVector8;
  vector<vectorType> myVector9;
  vector<vectorType> myVector10;
  return 0;
}
//example code 2
//this program declares 10 vectors of double
#include <vector>
#include <iostream>

typedef double vectorType;

int main() {
  vector<vectorType> myVector1;
  vector<vectorType> myVector2;
  vector<vectorType> myVector3;
  vector<vectorType> myVector4;
  vector<vectorType> myVector5;
  vector<vectorType> myVector6;
  vector<vectorType> myVector7;
  vector<vectorType> myVector8;
  vector<vectorType> myVector9;
  vector<vectorType> myVector10;
  return 0;
}

Compare the 2 examples. In them, I was able to change the declarations of 10 vectors from int to double by changing only one (1) line of code. Without the typedef, I would have had to edit 10 lines of code instead of just 1. What would have happened if I missed one?

"Shortening" probably isn't as good of a description as "simplifying". It's easier to remember a simpler name than it is a complicated one:

class SomeSampleClassThatIUseAllOverThePlaceInMyProgram {
};

typedef SomeSampleClassThatIUseAllOverThePlaceInMyProgram primaryDataClass;

So, finally, the whole and ONLY use of a typedef is to shorten or "simplify" a name?

Well, thanks for letting me know any ways(even though that's some thing I'd absolutely never consider).

I don't clearly get that

I understand, but you're not likely to get it without a bit more experience. This is just one of those features where one day you have an ah-ha! moment.

And as dumb as this sounds, what do you people mean by "types" and "portability?"

Type is short for data type. A data type specifies the possible values and operations that can be performed on an object. When you say int i , int is the data type of the object referred to by i . It means that i can be assigned integer values and used as an integer.

Portability is a measure of how much change is required to compile and run a program on a different platform. Whereas the hello world program should be 100% portable, a Win32 program will require a lot of changes to compile and run natively on Linux.

So, finally, the whole and ONLY use of a typedef is to shorten or "simplify" a name?

No. The whole and only use of typedef is to define an alias for a type. The alias need not be shorter or simpler.

I understand, but you're not likely to get it without a bit more experience. This is just one of those features where one day you have an ah-ha! moment.


Type is short for data type. A data type specifies the possible values and operations that can be performed on an object. When you say int i , int is the data type of the object referred to by i . It means that i can be assigned integer values and used as an integer.

Portability is a measure of how much change is required to compile and run a program on a different platform. Whereas the hello world program should be 100% portable, a Win32 program will require a lot of changes to compile and run natively on Linux.


No. The whole and only use of typedef is to define an alias for a type. The alias need not be shorter or simpler.

Okay, last thing....Is it ABSOLUTELY required in major programs?

Very little is ABSOLUTELY required, but a lot of things make your life as a programmer easier. You seem to be making a checklist of what you don't need, which is somewhat silly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.