Jump to content
• Advertisement

function that returns class template

This topic is 1333 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

hello, i have a class template

template <class _cType> class t3dpoint {
public:
_cType x;
_cType y;
_cType z;
};


now when it was just simple struct i could do

t3dpoint __fastcall vectorAB(t3dpoint A,t3dpoint B)
{
t3dpoint result;
result.x = B.x-A.x;
result.y = B.y-A.y;
result.z = B.z-A.z;
return result;
}


i came out with that thing i have no idea what should i write:

template <class type> t3dpoint<type> vectorAB(type A, type B)
{
t3dpoint<type>  result;
result.x = B.x-A.x;
result.y = B.y-A.y;
result.z = B.z-A.z;
return result;
}


so how should i deal with this

Share this post

Share on other sites
Advertisement

If you want to replicate the behavior of the first function, then your function has to take two points and not the primitive type it wraps. Your first function takes two objects of type t3dpoint and returns an object of type t3dpoint, so by extension the second function would have to take two t3dpoint<type> so that it can return a t3dpoint<type>.

Share this post

Share on other sites

ok but how to write this



template <class type> t3dpoint<type> vectorAB(t3dpoint<type> A, t3dpoint<type> B)

becasue i have no idea what to put before vectorAB(t3dpoint<type> A, t3dpoint<type> B)

Edited by WiredCat

Share this post

Share on other sites

Is there anything wrong with that line of code? Together with the class definition above, it looks just fine.

Share this post

Share on other sites

well it doesnt throw any errors on compile but i am changing the whole project to use templates and it needs lot of recoding so.. so i started another project and tried to use it

results drive me mad:



header

template <class _cType> class t3dpoint {
public:
_cType x;
_cType y;
_cType z;
};

cpp

template <class type> t3dpoint<type> vectorAB(t3dpoint<type> A, t3dpoint<type> B)
{
t3dpoint<type>  result;
result.x = B.x-A.x;
result.y = B.y-A.y;
result.z = B.z-A.z;
return result;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
t3dpoint <float> rA;
t3dpoint <float> rB;
rA.x = 0.0;
rA.y = -1.0;
rA.z = 0.2;

rB.x = 0.0;
rA.y = 0.0;
rA.z = 0.0;

t3dpoint <float> res = vectorAB(rA,rB);

}


res.x = 0

res.y = 3.07155392358416E33

res.z = 1.09585508442014E33

sometimes they change to different numbers

EDIT

now it works i had a bug in the code

rB.x = 0.0; rA.y = 0.0; rA.z = 0.0; instead of rB.y and rB.z ok it works

thanks for help!

Edited by WiredCat

Share this post

Share on other sites

Don't use underscores as a suffix.

Sorry to hijack a little, but I've seen header guards in the form of:

#ifndef __HEADER_HPP__
#define __HEADER_HPP__
#endif

Is that forbidden because it's using two adjacent underscores?

Share this post

Share on other sites

Don't use underscores as a suffix.

Using underscores as a suffix is ok, as long you avoid adjacent underscores.

What WiredCat is doing is using them as a prefix, which you shouldn't (as per ChaosEngine's quote).

Suffix = at the end.

Prefix = at the beginning.

Share this post

Share on other sites
Sorry to hijack a little, but I've seen header guards in the form of:
#ifndef __HEADER_HPP__
#define __HEADER_HPP__
#endif

Is that forbidden because it's using two adjacent underscores?

Technically, yes. It has adjacent underscrores which makes it a reserved name you shouldn't use.

Using underscores as a suffix is ok, as long you avoid adjacent underscores.

What WiredCat is doing is using them as a prefix, which you shouldn't (as per ChaosEngine's quote).

Suffix = at the end.

Prefix = at the beginning.

As per ChaosEngine's quote, WirdsCar's usage of the underscore is technically fine. It does not contain adjacent underscores, no leading underscore followed by an upper case letter, and no underscores at global scope. Not that I recommend using it anyway.

Share this post

Share on other sites

As per ChaosEngine's quote, WirdsCar's usage of the underscore is technically fine. It does not contain adjacent underscores, no leading underscore followed by an upper case letter, and no underscores at global scope. Not that I recommend using it anyway.

True, my mind just blanked out the "followed by uppercase" part.

Share this post

Share on other sites

• Advertisement

Announcements

• Advertisement
• Popular Tags

• Advertisement

• 11
• 20
• 12
• 10
• 37
• Forum Statistics

• Total Topics
631400
• Total Posts
2999860
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!