Jump to content
  • Advertisement
Sign in to follow this  
Monkan

Returning A Template Object From A Template Class

This topic is 2894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have a templated Vector3 class so I can have float, ints etc and I want a function which returns a Vector4 (also a templated class) of the same type.
I thought it would be as easy as:



// Convert to Vector4
Vector4<T> ToVector4()
{
return Vector4<T>(x, y, z, 1);
}

but this doesnt work???? any advice please.

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Try to be more specific than "doesn't work". Is there a compiler error? If so what is the exact text of the error?

Share this post


Link to post
Share on other sites
As an aside, I find your construction feels backwards to me - rather than having a "to vector4" on a vector3, I would have a "from vector3" on a vector4 taking the form of a constructor: Vector4<T>(const Vector3<T> & v)
As a bonus, this explicitly uses references to guarantee no redundant copying rather than relying on RVO which can be hit or miss.

Share this post


Link to post
Share on other sites
Thanks for the replies

SiCrane - sorry about that, yea getting a complier error.

It seems to not mind if I put a toVec4 function in my vector3 class but it doesnt like it if I put a toVec3 function in my vector4 class. I must have an error in my vector4 class somewhere.

dmatter - I think you are right, that does sound like a much better idea, dont know what I was thinking.

I've now got

Vector3(const Vector4<T> &vec4) : x(vec4.x), y(vec4.y), z(vec4.z) { }

in my Vector3 class. I must have a problem somewhere in my Vector4 which means its not compiling properly when I try to do the same thing. Weird.

Thanks for the help, I'll post again if I cant figure it out.

Share this post


Link to post
Share on other sites
Sorry, I didn't post the compile errors. I realised it was a mistake I had mad with my Vector4 class.
Because both classes were including each other I needed:


template <class T>
class Vector3;

at the top of my Vector4 class.
all sorted now.

Thanks for the help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!