Sign in to follow this  

typedef or not?

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

Its a C-ism, in C you cannot instantiate a struct type without using the struct keyword, the following is dis-allowed: name my_object;
Instead the instantiation should read: struct name my_object;
But if you use a typedef then you can ditch the struct keyword.

In C++ however this is a non-issue, you dont need to use the struct or class keywords to instantiate either of them.

So whether to use typedef in this way or not depends on the language you're using.

Share this post


Link to post
Share on other sites

typedef struct name { //stuff
} anothername;



same as:

struct name { //stuff
};
typedef anothername name;


?



typedef struct name { //stuff
} name;



same as:

struct name { //stuff
};
typedef name name; //prolly impossible, so totally useless


?

I'm confused :D

Share this post


Link to post
Share on other sites
Quote:
Original post by GreyHound
I'm confused :D


In both C and C++, struct something {} defines a structure named something, and that name becomes usable immediately. In both C and C++, typedef type name; associates name as an alias for the described type.

The difference is that in C++, structure names and typedef alias names exist within the same namespace (in the C sense, not the C++ sense) while in C there is a specific structure namespace independent from the namespace used by typedef aliases, functions and variables. One specifies the namespace by using the struct keyword before the name (so name would be looked for in the alias/function/variable namespace, while struct name would be looked for in the structure namespace).

Share this post


Link to post
Share on other sites

typedef struct name_s {
} name_t;



is the same as (in C):


struct name_s { };
typedef struct name_s name_t;



or the same as (in C++):


struct name_s { };
typedef name_s name_t;



and practically the same as (in C++):


struct name_t { };

Share this post


Link to post
Share on other sites
Quote:
Original post by rozz666
and practically the same as (in C++):

I would not us the phrase 'practically the same' unless you mean 'quite different.'

For example.

typedef struct S {
int i;
} T1;

class T2 {
int i;
};

void f1(S p)
{
}

void f2(T2 p)
{
}

void f3(T1 p) // error: already defined
{
}

int main()
{
S s;
T1 t1 = s; // okay, the same type

T2 t2 = s; // error: unrelated types.
}

Share this post


Link to post
Share on other sites

typedef struct S {
int i;
} T1;

class T2 {
int i;
};

<removed>

void f3(T1 p) // error: already defined
{
}

<removed>


I don't get an error for this in Visual C++ 2005 Express.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alquazar
I don't get an error for this in Visual C++ 2005 Express.


Bregma's example mistakenly gave the functions different names. The error obviously only happens if all functions have the same name. The error for VC++ being:

error C2084: function 'void f(S)' already has a body

Share this post


Link to post
Share on other sites
I use typedefing structs in C++ due to my C habits, I simply like such structs defs, and it doesn't influence performance. I also use very often (only for structs, never for classes) smth like this:
typedef struct {
(..)
} blah, *blahPtr;

It's quite usefull for me.

Quote:
void f3(T1 p) // error: already defined

Hmm not true ;> I think you meant void f1(T1 p) as for overloaded function - you have different function name here, no error will be returned ;)
Quote:
T2 t2 = s; // error: unrelated types.

As a matter of fact you are not (or shouldn't be allowed to do something like that:

T2 a, b;
(..)
a = b;

unless you overload = operator, and you can do it as well for (T2, T2) as for (T2, T1) ;>
Greets

[edit] You were quicker with this overloading ;p You answer too fast ;p

Share this post


Link to post
Share on other sites
Quote:
Original post by MSobiecki
As a matter of fact you are not (or shouldn't be) allowed to do something like that


You are allowed to, if the compiler generates the default operator=, as in Bregma's example [wink]

Share this post


Link to post
Share on other sites
Ah I see. I should've looked closer at what he was trying to show.

Although, from the POV of using T1 as the type and ignoring S, the examples are the same, right? Instead of having S as an equivalent type in the namespace, S would be replaced with something unnamed?

Share this post


Link to post
Share on other sites
Quote:
Original post by MSobiecki
IF it does, but as far as I know (ofc I may know wrong ;p) C++ language doesn't guarantee it, and if so, then it's not well practice to do like this.


Generation of the default assignment operator is guaranteed whenever it's possible, and it's possible whenever every member has an assignment operator with the correct signature.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
Quote:
Original post by rozz666
and practically the same as (in C++):

I would not us the phrase 'practically the same' unless you mean 'quite different.'

For example.

typedef struct S {
int i;
} T1;

class T2 {
int i;
};

void f1(S p)
{
}

void f2(T2 p)
{
}

void f3(T1 p) // error: already defined
{
}

int main()
{
S s;
T1 t1 = s; // okay, the same type

T2 t2 = s; // error: unrelated types.
}



I mean practically the same regarding the fact that you will be able to use name_t as a type.

Share this post


Link to post
Share on other sites

This topic is 3777 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this