Re: dynamic function definition
cesco wrote:
I need to define the body of a function in the constructor of
a class. For example, the class has a member function Log
(that is the public interface of the class), but I want the
body of this function to be defined as something or as
something else in the constructor of the class:
class MyClass
{
public:
void Log(const int& id, const int& value);
private:
void LogON(const int& id, const int& value);
void LogOFF();
};
MyClass::MyClass
{
// define here whether the function "Log" will behave as LogON or
LogOFF according to a switch
}
void MyClass::LogON(const int& id, const int& value)
{
// do something like push_back
}
void MyClass::LogOFF()
{
// do nothing
}
// don't know if I need or not the following definition
void MyClass::Log(const int& id, const int& value)
{
}
Any suggestion on how to do this?
Sounds like the strategy pattern to me. The function log() goes
through a delegate object created in the constructor; the
delegate object is basically an interface, with the different
implementations in different derived classes.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]