• 15
• 15
• 11
• 9
• 10

# function that returns class template

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

## 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 on other sites

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 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 on other sites

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

##### 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:



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 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__
#endif

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

##### 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 on other sites
Sorry to hijack a little, but I've seen header guards in the form of:
#ifndef __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 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.