Re: dynamic function definition
Hi cesco,
I didn't got your point what yuou are exactkly trying to do, but what
ever i have uinderstood, I have tried to program that.
Here I have created a static variable whcih changes its value in
Fucntion Log On and Log off. in Main , i have called it's constructor
twice, but in first case, it wil go in Log On and in second case , it
will go in Log Off.
if I am not wrong you were asking for this only behaviour.
See the Code Below
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass();
void Log(bool);
private:
void LogON();
void LogOFF();
static bool logon;
};
bool MyClass::logon = true;
MyClass::MyClass()
{
Log(logon);
}
void MyClass::LogON()
{
// do something like push_back
cout << "Now You are in LOGON"<<endl;
logon = false;
}
void MyClass::LogOFF()
{
// do nothing
cout << "Now You are in LOGOFF"<<endl;
logon = true;
}
// don't know if I need or not the following definition
void MyClass::Log(bool logon)
{
if (logon)
LogON();
else
LogOFF();
}
int main()
{
MyClass *clptr = new MyClass();
MyClass *clptr1 = new MyClass();
return 0;
}
Regards
...Pankaj
cesco wrote:
Hi,
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?
Thanks and regards
Francesco
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]