typedef struct ...

Started by
10 comments, last by stefu 21 years, 9 months ago
Hi!

Little question:

is this correct (it compiles & works allright)

typedef struct vec3 
{
	float x,y,z;
} vec3

I'm just concerned because I see everywhere doing it this way:

typedef struct _vec3 // <--- '_'
{
	float x,y,z;
} vec3

Why?

Use is for this:

typedef struct vec3
{
	float x,y,z;

#ifdef __cplusplus
	vec3() {}
	vec3(float x,float y,float z) { 
		this->x=x; this->y=y; this->z=z; 
	}
#endif

} vec3;

So the constructors need vec3 in the 'typedef struct vec3' line.
Nothing wrong there?

 
Thanks! [edited by - stefu on July 23, 2002 3:23:40 PM]
Advertisement
typedef struct vec3{  float x,y,z;} vec3; 

Under C++ you don''t need the typedef. Also, the above code should cause an error under C++ because it in effect declares a variable with the same identifier as the type, kinda like int int or string string.

My C''s too rusty to give you an accurate description.
Further to Oluseyi's answer:

You need the typedef in C because what you're actually declaring is this:

struct _Something {};  


Now, people get sick and tired of saying

struct _Something myStructName;struct _Something myStructName2;  


...for their variables. Therefore, you typedef it like any other:

typdef struct _Something { define your stuff here } something;  


The two names should be different and the convention is to say "tagXYZ" (e.g. "struct tagSomething") or to put an underscore there. Also note that because it's a shortcut you need to use the full name if you're doing something such as linked lists:

typedef struct tagSomething{  struct tagSomething* next; // <-- notice full name there} Something;  


You *don't* and *should not* put that typedef in for C++. In C++ structs are just like classes except that they start off in public rather than private mode (and default to public rather than private inheritance? I think).

[edited by - Alimonster on July 23, 2002 4:03:35 PM]
Thanks.

I use DevC++ and they compile just fine, in c and c++ I'v used it long with no problem :o

So what might be correct solution? It should work in c and c++ and have constructors in c++.

Maybe this?
#ifdef __cplusplusstruct vec3 {#elsetypedef struct tag_vec3 {#endif    float x,y,z;#ifdef __cplusplus    vec3() {}    vec3(float x,float y,float z) { ... }};#else} vec3;#endif  


Looks messy, is there simpler solution?


[edited by - stefu on July 23, 2002 4:51:16 PM]
WOHOO, guess what I found from MS DX SDK? This (in d3dxmath.h):

//--------------------------// 3D Vector//--------------------------typedef struct D3DXVECTOR3{#ifdef __cpluspluspublic:    D3DXVECTOR3() {};    D3DXVECTOR3( const float * );    D3DXVECTOR3( const D3DVECTOR& );    D3DXVECTOR3( float x, float y, float z );    // casting    operator float* ();    operator const float* () const;    operator D3DVECTOR* ();    operator const D3DVECTOR* () const;    operator D3DVECTOR& ();    operator const D3DVECTOR& () const;    // assignment operators    D3DXVECTOR3& operator += ( const D3DXVECTOR3& );    D3DXVECTOR3& operator -= ( const D3DXVECTOR3& );    D3DXVECTOR3& operator *= ( float );    D3DXVECTOR3& operator /= ( float );    // unary operators    D3DXVECTOR3 operator + () const;    D3DXVECTOR3 operator - () const;    // binary operators    D3DXVECTOR3 operator + ( const D3DXVECTOR3& ) const;    D3DXVECTOR3 operator - ( const D3DXVECTOR3& ) const;    D3DXVECTOR3 operator * ( float ) const;    D3DXVECTOR3 operator / ( float ) const;    friend D3DXVECTOR3 operator * ( float, const struct D3DXVECTOR3& );    BOOL operator == ( const D3DXVECTOR3& ) const;    BOOL operator != ( const D3DXVECTOR3& ) const;public:#endif //__cplusplus    float x, y, z;} D3DXVECTOR3, *LPD3DXVECTOR3; 


It is just like I have done it
quote:Original post by stefu
...is there simpler solution?

C++ structs that use no C++-specific features are backwards compatible with C (up to C99; the C maintainers have decided that compatibility with C++ is not a major consideration of theirs, so C++ may retaliate and finally be shed of its legacy). Just define the struct C-style, but omit the tag (it typedefs an unnamed struct to vec3, and works in C++ too).
quote:Original post by Oluseyi
Just define the struct C-style, but omit the tag (it typedefs an unnamed struct to vec3, and works in C++ too).


Like this?
typedef struct {    float x,yz;} vec3; 


But now I cannot put there constructors
"ANSI c++ forbids declaration ''vec3'' with no type"

Maybe I stay in my original way (also the MS way).
It works!

quote:Original post by Oluseyi
Also, the above code should cause an error under C++ because it in effect declares a variable with the same identifier as the type, kinda like int int or string string.

Not quite. It doesn''t declare a variable, only a type alias (i.e. vec3 becomes an alias for vec3). It''s completely unnecessary (in C++ that is), but still valid for C compatibility reasons.
If you want constructor, and your source file is .CPP, why not just throw away your typedef-combos?
Apart from knowing more, I don''t think these kind of construct really do something (accept that you want C-compatibility, which I don''t think that you are going for this).
"after many years of singularity, i'm still searching on the event horizon"
I''v come yet with another nice solution


  // first pure c solutiontypedef struct _vec3{    float x,y,z;} vec3;// Derived class with c++ featuresclass Vector3 : public vec3 {public:    // default constructor    Vector3() {};    // another constructor    Vector3(float _x,float _y,float _z) { x=_x; y=_y; z=_z;};    // copy constructor to allow: Vector3 a=v3; (where v3 is a vec3)    Vector3(const vec3& v) { x=v.x; y=v.y; z=v.z;};    // put here all operators...};  


I think that this should work pretty well

This topic is closed to new replies.

Advertisement