Skip to main content
gamedev.net
🔒 Locked

typedef struct ...

Started by stefu 10 replies 1.6k views
Original Post
stefu
stefu
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]
Oluseyi
Oluseyi

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.
Alimonster
Alimonster
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]
stefu
stefu
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 __cplusplus
struct vec3 {
#else
typedef 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]
stefu
stefu
WOHOO, guess what I found from MS DX SDK? This (in d3dxmath.h):


//--------------------------
// 3D Vector
//--------------------------
typedef struct D3DXVECTOR3
{
#ifdef __cplusplus
public:
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
Oluseyi
Oluseyi
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).
stefu
stefu
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!

Dactylos
Dactylos
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.
DerekSaw
DerekSaw
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 n the event horizon" o
stefu
stefu
I''v come yet with another nice solution


  
// first pure c solution

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

// Derived class with c++ features

class 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
davepermen
davepermen
why use classes and constructors and all that if you want c-portability? rather stupid. stick to c++..


      
struct vec {
float x,y,z;
vec(){}
vec(float _x,float _y,float _z):x(_x),y(_y),z(_z) {}
vec operator + (const vec& _other) {
return vec(x+_other.x,y+_other.y,z+_other.z);
}
};


note: i use _ for functionparameters, and m_ for member variables, except in this one, because i want to access x,y and z directly, and the m_ is just overhead in this case..

"take a look around" - limp bizkit
www.google.com

edit: i just edited a typo in the text, source remains the same

[edited by - davepermen on July 25, 2002 10:41:24 AM]
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
stefu
stefu
quote:
Original post by davepermen
why use classes and constructors and all that if you want c-portability? rather stupid. stick to c++..


This is a question that I''v been thinking of and just before I came here to read your answer I had decided to convert it fully to c++.

Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.