N
N
Nikita Gusakov2013-05-02 02:04:20
C++ / C#
Nikita Gusakov, 2013-05-02 02:04:20

How to overload a function inside a template?

template <typename T> class matrix
{
private:
    T **m_container;
    int m_rows;
    int m_columns;
    
    void in(int i, int j)
    {
        cin >> m_container[i][j];
    }
    template <char*> void in(int i, int j)
    {
        m_container[i][j] = new char [100];
        cin.getline(m_container[i][j], 100);
    }

Here is a piece of code, I think it's clear what I wanted to get. If T is char*, then in is overloaded, but something is not very good.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vScherba, 2013-05-02
@hell0w0rd

From what I understand, you need to specialize in for matrix<char*>:

template <typename T> class matrix
{
private:
    T **m_container;
    int m_rows;
    int m_columns;
    
    void in(int i, int j)
    {
        cin >> m_container[i][j];
    }
};

template<>
void matrix<char*>::in(int i, int j)
{
    m_container[i][j] = new char [100];
    cin.getline(m_container[i][j], 100);
}][j], 100);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question