typedef or not?

Started by
16 comments, last by rozz666 16 years, 8 months ago
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
Advertisement
Quote:Original post by Alquazar
I don't get an error for this in Visual C++ 2005 Express.

And you shouldn't, I have a feeling Bregma meant: void f1(T1 p) // error: already defined


Edit: What ToohrVyk said [grin]
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
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]

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.
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?
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.

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.

This topic is closed to new replies.

Advertisement