views:

116

answers:

2

Hi , i have template class that I did , and i want to do pointer to function:

#ifndef __DATABASE_H_
#define __DATABASE_H_
#pragma once
#include "Heap.h"
#include <string>
#include <map>
#include <iostream>



using namespace std;

template<class S,class T> class Database {
public:
    bool IsEmpty() const { return (m_map.size() == 0); } //O(1)
    void Insert(T toAdd); //log(n)
    void UpDate (S KeyToUpDate); //log(n)
    T Remove (const S keyToRemove); //log(n)
    T RemoveMin (); //log(n)
    T GetMin () const; //O(1)
    int GetNumOfElements () const { return m_map.size(); }//0(1)
    void ShowByInputOrder () const; //O(1)


private:
    map<S,Node<T>*> m_map;
    Heap<T> m_minHeap;
};

template<class S,class T> 
void Database<S,T>::Insert(T toAdd) {
    Node<T>* nodeToHeap = new Node<T>(toAdd);
    Node<T>* nodeToMap = new Node<T>(toAdd);
    nodeToHeap->SetBrother(nodeToMap);
    nodeToMap->SetBrother(nodeToHeap);
    m_minHeap.InsertNode(nodeToHeap);
    m_map.insert(make_pair((S)nodeToMap->GetData()->GetLicenseId(),nodeToMap));
}

#endif

instand :

template<class S,class T> 
void Database<S,T>::Insert(T toAdd, /*pointer to function here*/) {
    Node<T>* nodeToHeap = new Node<T>(toAdd);
    Node<T>* nodeToMap = new Node<T>(toAdd);
    nodeToHeap->SetBrother(nodeToMap);
    nodeToMap->SetBrother(nodeToHeap);
    m_minHeap.InsertNode(nodeToHeap);
    m_map.insert(make_pair(/*pointer to function here*/,nodeToMap));
}

in the main :

.
..
Vehicle *car1 = new Car ("300-900-50","Fiat",4);
database<string,Vehicle*> m_vec;
m_vec.insert (car1,car1->GetLicenseId());
.
..
...
}

how i do pointer to function ??

A: 

Instead of using function pointers, you should instead use function objects (commonly refered to as functors). If you need to store these functors for later use (such as in a map), they should all inherit from a common abstract base class so you can use that as the value type for your map.

Here is an example that makes use of two simple functors which could easily be extended to include a return type.

#include <iostream>
#include <string>

using namespace std;

class PrintStringFunctor {
    string var;

public:
    PrintStringFunctor(string const & s) 
        : var(s) {}

    void operator()() {
        cout << var << endl;
    }
};

class PrintReverseStringFunctor : public PrintStringFunctor {
public:
    PrintReverseStringFunctor(string const & s) 
        : PrintStringFunctor(string(s.rbegin(), s.rend())) {}
};


template <typename T>
void Run(T f) {
    f();
}

int main() {
    Run(PrintStringFunctor("Hello, world!"));
    Run(PrintReverseStringFunctor("Hello, world!"));

    return 0;
}

Outputs:

Hello, world!
!dlrow ,olleH
Paul
A: 

In order to get a pointer to a member function, it must be static. I can't see how useful this would be, as you'd have to explicity template every pointer declaration you want, which would significantly reduce the flexibility offered by a function pointer.

edit: a commentor points something out. I am incorrect that the function must be static; that was a blatantly false generalization. However, you must still name the class in front of the pointer, which is more or less what I was getting at. Having to know the type of the class which the function is a member negates much of the usefulness of traditional purposes of function pointers.

San Jacinto
You can get the address of non-static member functions too, you then just need an instance to call it on.
Georg Fritzsche
@georg dully noted, thanks.
San Jacinto