Jun 16, 2012 at 9:07am UTC
Hi everybody!
You know... recently I started a research on Templates, and I found this, but I don't know exactly what it has to do, it is part of a test I'm going to do for school
The only advice that was given to me:
"What are the conditions in order for this code to work"
I'm assuming that it has to add the values from cVector3df in a new object created from a template.
What topics should I research in order to learn about this weird Class Templates?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;
template <class T>
class ClasaTeste
{
private :
T m_var;
public :
void Add(T newVar)
{
m_var += newVar;
}
void Addl(T newVar)
{
m_var = m_var + newVar;
}
};
class cVector3df
{
float x,y,z;
};
int _tmain(int argc, _TCHAR* argv[])
{
ClasaTeste<cVector3df> t;
cVector3df p;
t.Add(p);
t.Addl(p);
system("PAUSE" );
return 0;
}
Last edited on Jun 16, 2012 at 9:12am UTC
Jun 16, 2012 at 9:19am UTC
You just need to add operator overloading to class cVector3df like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class cVector3df
{
float x,y,z;
public :
cVector3df& operator + (const cVector3df& rhs)
{
cVector3df out;
out.x = x + rhs.x;
out.y = y + rhs.y;
out.z = z + rhs.z;
return out;
}
void operator += (const cVector3df& rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
}
};
Last edited on Jun 16, 2012 at 9:28am UTC
Jun 16, 2012 at 10:23am UTC
It does work now, but it says that i'm using the variable "p" without being initalized.
I'm researching about operator overloading.
cVector3df& operator + (const cVector3df& rhs)
is our constructor? how does it come to overload? why?
can we make a constructor for cVector3df, giving our variables a default value and having our operator overloaded?
I'm kinda confused...
Jun 16, 2012 at 10:47am UTC
Here's the code again, but I'm not sure about Template Specialization (see code below)
If i'm having <class T> , how can I make a constructor so it takes "R" float, int, or anything else as a template?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;
template <class T>
class ClasaTeste
{
private :
T m_var;
public :
//ClasaTeste(){} <- how can I make a constructor here
//so I can initialize its variables?
//I mean, how can I do a Template specialization here?,
//if I do already have "<class T>"
void Add(T newVar)
{
m_var += newVar;
}
void Addl(T newVar)
{
m_var = m_var + newVar;
}
};
class cVector3df
{
float x,y,z;
public :
cVector3df(){}
cVector3df(float numX, float numY, float numZ)
{
x = numX;
y = numY;
z = numZ;
}
cVector3df& operator + (const cVector3df& rhs)
{
cVector3df out;
out.x = x + rhs.x;
out.y = y + rhs.y;
out.z = z + rhs.z;
return out;
}
void operator += (const cVector3df& rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
ClasaTeste<cVector3df> t;
cVector3df p(1,2,3);
t.Add(p);
t.Addl(p);
system("PAUSE" );
return 0;
}
Last edited on Jun 16, 2012 at 10:48am UTC
Jun 16, 2012 at 12:15pm UTC
cVector3df& operator + (const cVector3df& rhs)
Warning: returning reference to local variable.
how can I make a constructor so it takes "R" float, int, or anything else as a template?
You put float, int, or anything else between the <>.
Maybe I didn't understand your question, ¿could you exemplify?
Last edited on Jun 16, 2012 at 12:15pm UTC