Author Topic: [C++] What is typedef  (Read 5047 times)

0 Members and 1 Guest are viewing this topic.

Offline James_wright_1990

  • C= 64
  • **
  • Posts: 48
  • Karma: 4
    • View Profile
[C++] What is typedef
« on: December 16, 2009 »
I'm in the processes of moving alot of my code across to C++ and i have been using the nehe tutorials to help with coding some of my c# opengl stuff int C++ and i have come across something called typedef. Up until now i have been ok because alot of c++ is similare to C# but this one confuses me. I usually just go back to my lecture notes but it doesnt seem to be in thereand the stuff i found on the internet is a little confusing is see code like:

Code: [Select]
typedef struct
{
//code
}
nameofstuct

what is the difference between that and:

Code: [Select]
struct nameofstruct
{
//code
}

if anyone can shed somelight on this i would greatly appreaciat it.

James
"if debugging is the proccess of removing bugs from software then that means that programming must be the proccess of putting them in" my favourite programming quote.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] What is typedef
« Reply #1 on: December 16, 2009 »
In C++ struct and class are very similar, and similar to the same things in C#.
In C++ typedef is a way of creating an alias for a struct - it can't have any code in it.
So you can do
typedef struct
{
  double real;
  double imaginary;
} complex_number;

and then you can use
complex_number z;
to declare one.
This saves you having to write
struct
{
  double real;
  double imaginary;
} z;
I'm not sure if having 'class' in the language means that typedef is redundant, I'm more of a C programmer :)

Jim
Challenge Trophies Won:

Offline James_wright_1990

  • C= 64
  • **
  • Posts: 48
  • Karma: 4
    • View Profile
Re: [C++] What is typedef
« Reply #2 on: December 16, 2009 »
Ah thanks for that that makes much more sense now i was getting confused by the examples but i think i get it now. I'm more used to using classes so i think i'll stick with that for now but its good to know what it means when i see it in the examples.

Cheers for the help.

James
"if debugging is the proccess of removing bugs from software then that means that programming must be the proccess of putting them in" my favourite programming quote.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] What is typedef
« Reply #3 on: December 16, 2009 »
Code: [Select]
typedef struct
{
//code
}
nameofstuct

what is the difference between that and:

Code: [Select]
struct nameofstruct
{
//code
}
In C++ the "typedef" is redundant but is often used for compatibility reasons because in C (not C++) the "struct" keyword is required for every instantiation:
Code: [Select]
struct Vector3D {
  float x,y,z;
};

struct Vector3D v;
As this is quite annoying you can create an alias using "typedef":
Code: [Select]
typedef   struct Vector3D   Vector;So now you can simply write:
Code: [Select]
Vector v;Actually we aren't interested in "struct Vector3D" anymore, so we don't even supply a name for it:
Code: [Select]
typedef   struct{float x,y,z;}  Vector;
« Last Edit: December 16, 2009 by hellfire »
Challenge Trophies Won:

Offline James_wright_1990

  • C= 64
  • **
  • Posts: 48
  • Karma: 4
    • View Profile
Re: [C++] What is typedef
« Reply #4 on: December 16, 2009 »
In C++ the "typedef" is redundant but is often used for compatibility reasons because in C the "struct" keyword is required for every instantiation:
Code: [Select]
struct Vector3D {
  float x,y,z;
};

struct Vector3D v;
As this is quite annoying you can create an alias using "typedef":
Code: [Select]
typedef   struct Vector3D   Vector;So now you can simply write:
Code: [Select]
Vector v;Actually we aren't interested in "struct Vector3D" anymore, so we don't even supply a name for it:
Code: [Select]
typedef   struct{float x,y,z;}  Vector;

ah i see that deos make the code look a lot nicer. My lecturers this year seem to not like structs (whish i can undersatnd) so i dont use them very often but i do use them occasionally and I'll definately use typedef now i know what it does

thanks
James
"if debugging is the proccess of removing bugs from software then that means that programming must be the proccess of putting them in" my favourite programming quote.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] What is typedef
« Reply #5 on: December 16, 2009 »
In C++ "struct" has exactly the same features as "class".
The only difference is that by default "struct" is public and "class" is private.
"class" sounds more oop, though.

"typedef" becomes rather useful when handling nested templates and types start to look like this:
Code: [Select]
std::map< std::pair<std::string, std::string>, std::vector<std::string> >
« Last Edit: December 16, 2009 by hellfire »
Challenge Trophies Won:

Offline James_wright_1990

  • C= 64
  • **
  • Posts: 48
  • Karma: 4
    • View Profile
Re: [C++] What is typedef
« Reply #6 on: December 19, 2009 »
Ah right so in C++ is there not as clear a distinction between a class and a struct. Im used to C# where there is a deffinate difference. if there isn't a difference why do my lecturers seem to dislike structs so much?

James
"if debugging is the proccess of removing bugs from software then that means that programming must be the proccess of putting them in" my favourite programming quote.

Offline asdflkj

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Re: [C++] What is typedef
« Reply #7 on: April 25, 2010 »
-
« Last Edit: August 06, 2025 by marclurr »

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] What is typedef
« Reply #8 on: April 26, 2010 »
As for classes and structs; the same difference that exists in C# applies here. That is structs only hold data, whereas classes can also have 'behaviour' (functions/methods).
No, in C++ "struct" can make use of the whole oop stuff.
Challenge Trophies Won: