Click here to Skip to main content
15,923,789 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: std::string transformation Pin
Joaquín M López Muñoz20-May-02 21:03
Joaquín M López Muñoz20-May-02 21:03 
GeneralPointer to member function Pin
Aaron Schaefer20-May-02 10:50
Aaron Schaefer20-May-02 10:50 
GeneralRe: Pointer to member function Pin
Joaquín M López Muñoz20-May-02 10:58
Joaquín M López Muñoz20-May-02 10:58 
GeneralRe: Pointer to member function Pin
Aaron Schaefer21-May-02 3:34
Aaron Schaefer21-May-02 3:34 
GeneralRe: Pointer to member function Pin
Chris Losinger20-May-02 11:04
professionalChris Losinger20-May-02 11:04 
GeneralRe: Pointer to member function Pin
Aaron Schaefer21-May-02 3:31
Aaron Schaefer21-May-02 3:31 
GeneralRe: Pointer to member function Pin
Chris Losinger21-May-02 5:40
professionalChris Losinger21-May-02 5:40 
GeneralRe: Pointer to member function Pin
Aaron Schaefer21-May-02 7:07
Aaron Schaefer21-May-02 7:07 
Right, I know which object, but I don't know which function. I just need to be able to call functions by name. Like this:

class CBase
{
protected:

typedef double (CBase::*MemberFn)();

std::map<std::string, memberfn=""> m_map;
void RegisterFunction(const std::string& strName, MemberFn);

public:
CBase();
virtual ~CBase();

double Function(const std::string& strName);

virtual double VFunc() {return 1.00;}

};

// Implementation of base
// Base.cpp: implementation of the CBase class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Base.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBase::CBase()
{
double (CBase::*pFunc)() = NULL;
}

CBase::~CBase()
{

}

void CBase::RegisterFunction(const std::string& strName, MemberFn)
{
m_map[strName] = pFunc;
}

double CBase::Function(const std::string& strName)
{
double rv;
double (CBase::*pFunc)() = m_map[strName];

rv = ((*this).*pFunc)();
return rv;
}

// And a derived class
class CDerived : public CBase
{
public:
CDerived();
virtual ~CDerived();

double Pi() {return 3.1415;}

virtual double VFunc() {return 2.00;}
};


CDerived::CDerived()
{
double x = 0.00;
CBase::RegisterFunction("Pi", (double (CBase::*)(void))&CDerived::Pi);
CBase::RegisterFunction("VF", (double (CBase::*)(void))&CDerived::VFunc);

}

CDerived::~CDerived()
{

}


// And a test driver

int main(int argc, char* argv[])
{
CBase* pBase = new CDerived();

double rv = pBase->Function("Pi");
rv = pBase->Function("VF");

printf("Hello World!\n");
return 0;
}


The idea is to reproduce the functionality of the IDispatch::Invoke method, but still be sort of portable. Kind of like a dispatch map, sort of. In other words, the base class can call functions of the derived class, by name. I could make a calculator or something like that, without having to hard code any function calls. Functions could be added without changing the interface seen by client code.

It seems like there's not a real good reason to want to do this, but there are instances where this sort of functionality is useful. Think of an object that implemented a whole bunch of functions, maybe a class called CMath, then you coul fairly easily write a command line program that understood the following:

sin(4.5) + cos(34.5) - atan(2) + 1.23

Well, you would have to write something to transpose this into RPN or something, so the precedence of operations was preserved correctly. This is not what I am needing the functionality for, but could be used for that sort of thing.

Aaron
GeneralRe: Pointer to member function Pin
Aaron Schaefer21-May-02 7:09
Aaron Schaefer21-May-02 7:09 
GeneralRe: Pointer to member function Pin
Chris Losinger21-May-02 8:09
professionalChris Losinger21-May-02 8:09 
GeneralRe: Pointer to member function Pin
Aaron Schaefer21-May-02 8:17
Aaron Schaefer21-May-02 8:17 
GeneralRe: Pointer to member function Pin
James R. Twine20-May-02 11:00
James R. Twine20-May-02 11:00 
GeneralRe: Pointer to member function Pin
Ernest Laurentin20-May-02 11:14
Ernest Laurentin20-May-02 11:14 
GeneralRe: Pointer to member function Pin
Aaron Schaefer21-May-02 3:33
Aaron Schaefer21-May-02 3:33 
GeneralRe: Pointer to member function Pin
Aaron Schaefer21-May-02 3:40
Aaron Schaefer21-May-02 3:40 
GeneralRe: Pointer to member function Pin
hex20-May-02 16:18
hex20-May-02 16:18 
GeneralRe: Pointer to member function Pin
Aaron Schaefer21-May-02 3:42
Aaron Schaefer21-May-02 3:42 
GeneralSetItemData() and DWORD_PTR Pin
dazinith20-May-02 10:37
dazinith20-May-02 10:37 
GeneralRe: SetItemData() and DWORD_PTR Pin
Joaquín M López Muñoz20-May-02 10:51
Joaquín M López Muñoz20-May-02 10:51 
GeneralConnect method of Winsock Control Pin
20-May-02 10:35
suss20-May-02 10:35 
GeneralRe: Connect method of Winsock Control Pin
Rickard Andersson2020-May-02 11:29
Rickard Andersson2020-May-02 11:29 
GeneralOO SubClassProc Pin
S van Leent20-May-02 9:54
S van Leent20-May-02 9:54 
GeneralRe: OO SubClassProc Pin
Joaquín M López Muñoz20-May-02 10:19
Joaquín M López Muñoz20-May-02 10:19 
QuestionHow to make .exe file take parameters in VC++? Pin
Shayna20-May-02 9:16
Shayna20-May-02 9:16 
AnswerRe: How to make .exe file take parameters in VC++? Pin
redeemer20-May-02 9:18
redeemer20-May-02 9:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.