I'm currently doing practicing on the ->* operator with the intent to write a smart pointer. I've done the basics on how it works. For this example I want to use templates when using operator->*(), so I can use member functions for a variety of return and paramater types.
Below is a simple example of my objective
Functor.h
#pragma once
#include "RecordCard.h"
template <class OBJECT_TYPE, typename POINTER_TO_MEMBER>
class Functor{
public:
Functor(OBJECT_TYPE* pObj, POINTER_TO_MEMBER pMF):m_pObj(pObj),m_pMF(pMF){}
template <typename RETURN_TYPE>
RETURN_TYPE operator() const
{
return (m_pObj->*m_pMF)();
}
template <typename PARAM_TYPE>
void operator()(PARAM_TYPE param)
{
(m_pObj->*m_pMF)(param);
}
private:
OBJECT_TYPE* m_pObj;
POINTER_TO_MEMBER m_pMF;
};
RecordCard.h
#pragma once
#include "Functor.h"
#include <string>
template <class T, typename U>
class Functor;
class RecordCard{
public:
RecordCard(){}
void SetName(std::string);
void SetAge(unsigned int);
void SetActiveStatus(bool);
std::string GetName() const;
unsigned int GetAge() const;
bool GetActiveStatus() const;
// Other methods of the class
template <typename T>
Functor<RecordCard,T> operator->*(T pmf)
{
return Functor<RecordCard,T>(this, pmf);
}
template <typename T, typename U = T (RecordCard::*)() const>
const Functor<RecordCard,U> operator->*(U pmf) const
{
return Functor<RecordCard,U>(this, pmf);
}
private:
std::string m_szName;
unsigned int m_nAge;
bool m_bActive;
};
RecordCard.cpp
// Method definitions here
// RecordCard::operator->* definition removed from her
// and placed in the header file. Because to otherwise
// causes the linker to complain.
Now the problem lies in my main. Main.cpp
#include "RecordCard.h"
#include "Functor.h"
int main()
{
RecordCard mycard;
(mycard->*&RecordCard::SetAge)(30); // Works Okay
(mycard->*&RecordCard::GetAge)(); Error??
return 0;
}
The two complaints the compiler are giving me are:
Unable to find a matching signature for RecordCard::GetAge() const.
Unable to create an instance of Functor<RecordCard, int (RecordCard::*)()>
The reason why it's happening it's not calling operator->*() const.
Without using lambdas or std::function, those are for future excercises. How can resolve this so it works correctly.
Many thanks.