yaazz

Jan 23, 2009, 01:18 PM

Hey there, I am making a vector class in objective C and would like to make it as expandable as possible.

I would like to have a 2D version of the class, and a 3D version, and have the two classes able to work with each other interchangably. The only difference is that the 3d version will have functions to deal with cross products and stuff like that.

To do this, I made a protocol with all of the basic functions a vector would need. Then, I will just add additional classes into the interface for Vector3d.

But I can't figure out how to specify that I want the protocol to work with anything that implements the protocol.

I can explain better by just putting the protocol here

@protocol vecProt

+createAtX:Y:Z:; //class variable to create a vector at <X,Y,Z>

+add:To:; //class variable to add two vectors

+subtract:From:; //class variable to subtract 2 vectors

-(id) init;

-(id) add:; //adds this vector to another

-(id) subtract:; //subtracts this vector from another

-(id) dotProductWith:;//returns te dot product of this vector with another

-(id) length; //Returns the length of the vector

-(id) lengthSquared; //Returns the Squared Length of the vector

@end

right now I am returning id, but would like to say something along the lines of "return a vector", how can I do this exactly?

also, how do I specify what I can add and subtract from after the ":" in the method names?

I would like to have a 2D version of the class, and a 3D version, and have the two classes able to work with each other interchangably. The only difference is that the 3d version will have functions to deal with cross products and stuff like that.

To do this, I made a protocol with all of the basic functions a vector would need. Then, I will just add additional classes into the interface for Vector3d.

But I can't figure out how to specify that I want the protocol to work with anything that implements the protocol.

I can explain better by just putting the protocol here

@protocol vecProt

+createAtX:Y:Z:; //class variable to create a vector at <X,Y,Z>

+add:To:; //class variable to add two vectors

+subtract:From:; //class variable to subtract 2 vectors

-(id) init;

-(id) add:; //adds this vector to another

-(id) subtract:; //subtracts this vector from another

-(id) dotProductWith:;//returns te dot product of this vector with another

-(id) length; //Returns the length of the vector

-(id) lengthSquared; //Returns the Squared Length of the vector

@end

right now I am returning id, but would like to say something along the lines of "return a vector", how can I do this exactly?

also, how do I specify what I can add and subtract from after the ":" in the method names?