Thread: Something about aggregate type, apparently.

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    Something about aggregate type, apparently.

    This may sound odd or just impossible...

    I'm doing a lot of work with geometry-like user defined types (structures), for example
    Code:
    struct point2d{double x,y;};
    struct point3d{double x,y,z;};
    And I would be REALLY happy to be able to use syntax like this to operate on them:
    Code:
    point3d blah(point3d k);
    {return {k.x*2,k.y*2,k.z*2};}
    ... ... ... ... ... ...
    point3d t;
    t={0,0,0};
    t=blah({1,1,1});
    Please notice:
    >the construct in the return statement of the function
    >the assignment of t
    >the function call

    So far I'm aware that I can write
    Code:
    struct point3d t={0,0,0};
    According to MSDN, this is called "aggregate type initialization" and I was very happy when I found it, believing that I found some alternative syntax that would help me construct types easily. But I was wrong, the syntax doesn't work in the above examples (I exposed three situations where I was expecting it to work).
    So, why doesn't it work? What do you think I should do? So far I'm using these "constructor functions" that I made:
    Code:
    point2d _stdcall fpoint2d(double x,double y)
    {struct point2d t={x,y};
    return t;}
    point3d _stdcall fpoint3d(double x,double y,double z)
    {struct point3d t={x,y,z};
    return t;}
    I'm very happy with these regarding the syntax, but ... I'm concerned that using them will slow down my calculations; and time is a problem.
    Should I enhance the types to classes? Would'n that be slower? Should I use custom operators?
    So, what are my options?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Here is a 3d vertex struct I use:
    Code:
    struct CVector3
    {
    public:
    	
    	// A default constructor
    	CVector3() {}
    
    	// This is our constructor that allows us to initialize our data upon creating an instance
    	CVector3(float X, float Y, float Z) 
    	{ 
    		x = X; y = Y; z = Z;
    	}
    
    	// Here we overload the + operator so we can add vectors together 
    	CVector3 operator+(CVector3 vVector)
    	{
    		// Return the added vectors result.
    		return CVector3(vVector.x + x, vVector.y + y, vVector.z + z);
    	}
    
    	// Here we overload the - operator so we can subtract vectors 
    	CVector3 operator-(CVector3 vVector)
    	{
    		// Return the subtracted vectors result
    		return CVector3(x - vVector.x, y - vVector.y, z - vVector.z);
    	}
    	
    	// Here we overload the * operator so we can multiply by scalars
    	CVector3 operator*(float num)
    	{
    		// Return the scaled vector
    		return CVector3(x * num, y * num, z * num);
    	}
    
    	// Here we overload the / operator so we can divide by a scalar
    	CVector3 operator/(float num)
    	{
    		// Return the scale vector
    		return CVector3(x / num, y / num, z / num);
    	}
    
    	CVector3 operator=(CVector3 v)
    	{
    		return CVector3(v.x, v.y, v.z);
    	}
    
    	float x, y, z; 						
    };
    You can easily write functions in the struct to do things like double your values, or whatever. When you declare a struct or class you don't put 'struct' or 'class' before you declare the specific struct or class.

    When you put:
    Code:
    struct point3d t={0,0,0};
    you just put

    point3d t=(0,0,0);

    If you don't want all the addition and stuff you can just delete that to save some mem, but it won't really slow it down much.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    Thanks, you were helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Segmentation Fault?
    By John_L in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 08:37 AM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM