dor_torge
dor_torge

Reputation: 69

Template as parameter in class

I have header file and in my header file I make a template and I want to use the template just on one function and not force all other functions. Is it possible to get the type before the function like i did in main? This is an example:

TestTemp.h

// TestTemp.h
#ifndef _TESTTEMP_H_
#define _TESTTEMP_H_
template<class T>
class TestTemp  
{
public:
    TestTemp();
    void SetValue( int obj_i );
    int Getalue();
    void sum(T b, T a);

private:
    int m_Obj;
};
#include "TestTemp.cpp"

#endif

TestTemp.cpp

//TestTemp.cpp
include<TestTemp.h>
TestTemp::TestTemp()
{
}
void TestTemp::SetValue( int obj_i )
{
    m_Obj = obj_i ;
}

int TestTemp::GetValue()
{
    return m_Obj ;
}

template<class T>
void TestTemp<T>::sum(T b, T a)
{
    T c;
    c = b + a;
}

main.cpp

//main.cpp
include<TestTemp.h>
void main()
{
    TestTemp t;
    t.sum<int>(3,4);
}

Have any ideas?

Upvotes: 1

Views: 110

Answers (4)

Ha Tran
Ha Tran

Reputation: 1

You just need to define TestTemp as normal class of which "sum" is a template function. Then in your function "main" (i.e. the caller), the template argument will be deduced from the function arguments.

class TestTemp
{
public:
    TestTemp();
    void SetValue(int obj_i);
    int Getalue();

    template<class T>
    void sum(T b, T a);

private:
    int m_Obj;
};

TestTemp::TestTemp() {}

void TestTemp::SetValue(int obj_i)
{
    m_Obj = obj_i;
}

int TestTemp::Getalue()
{
    return m_Obj;
}

template<class T>
void TestTemp::sum(T b, T a)
{
    T c;
    c = b + a;
}


//main.cpp


int main()
{
    TestTemp t;
    t.sum(3, 4);
    return 0;
}

Upvotes: 0

Peter Ittner
Peter Ittner

Reputation: 551

You should take a look here. There are many examples.

I think it should work as you expect it if you just remove

template<class T>

from the

#ifndef _TESTTEMP_H_
#define _TESTTEMP_H_
--> template<class T> <--
class TestTemp  

block. You don't have to define the whole class as templated when you just want a method to have template parameters.

Upvotes: 0

billz
billz

Reputation: 45470

Your TestTemp is a template class already, no need to make sum template function.

TestTemp<int> t;
t.sum(3, 4);

If you really want to make sum function a template function of TestTemp:

template<class T>
class TestTemp  
{
public:
    //....
    template<typename U>
    void sum(U b, U a);

private:
    int m_Obj;
};

To implement it outside template class:

template<class T>
template<typename U>
void TestTemp<T>::sum(U b, U a)
{
    T c;
    c = b + a;
}

int main()
{
    TestTemp<int> t;
    t.sum<int>(3, 4);
}

However, I feel you just need a free template function

template<typename T>
T sum(T a, T b)
{ return a + b; }

Upvotes: 1

Mochimazui
Mochimazui

Reputation: 132

// TestTemp.h

#ifndef _TEST_TEMP_H_
#define _TEST_TEMP_H_

class TestTemp
{
public:
    TestTemp();
    void SetValue( int obj_i );
    int Getalue();

    template<class T>
    void sum(T b, T a);

private:
    int m_Obj;
};

TestTemp::TestTemp() {}

void TestTemp::SetValue( int obj_i )
{
   m_Obj = obj_i ;
}

int TestTemp::Getalue()
{
    return m_Obj ;
}

template<class T>
void TestTemp::sum(T b, T a)
{
   T c;
   c = b + a;
}

#endif

//main.cpp
#include <TestTemp.h>

int main()
{
    TestTemp t;
    t.sum<int>(3,4);
    return 0;
}

What you need is a normal class which has a template member function.

It's not a god idea to include an cpp file in header file. For template functions, just put it in header file.

Upvotes: 0

Related Questions