function that returns class template

Started by
10 comments, last by ChaosEngine 9 years, 5 months ago

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

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>.

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)

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

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!

Don't use underscores as a suffix prefix. note to self: don't post without coffee.

  • Reserved in any scope, including for use as implementation macros:
    • identifiers beginning with an underscore and an uppercase letter
    • identifiers containing adjacent underscores (or "double underscore")
  • Reserved in the global namespaces:
    • identifiers beginning with an underscore
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

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?

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty


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.

Hello to all my stalkers.

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.


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.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement