By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

dynamic function definition

P: n/a
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

Jul 11 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"cesco" <fd**********@gmail.comwrote:
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?
Yes. Use an interpreted language such as APL or Perl.
On-the-fly self-reprogramming programs are impossible in
a compiled language such as C++.

But you really don't need "dynamic function definition" for
what you describe. Polymorphism would work for you, I think.

Something like:

class MyLogBase // Abstract base; can't be instantiated
{
public:
// (contstructors, etc.)

// Pure Virtual Function:
virtual void Log(const int& id, const int& value) = 0;

private:
// etc.
};

class MyLogOn : public MyLogBase
{
public:
// (contstructors, etc.)

// over-ride the pure virtual function from the base:
void Log(const int& id, const int& value)
{
std::cout << "LOG ON" << std::endl;
}
private:
// etc.
};

class MyLogOff : public MyLogBase
{
public:
// (contstructors, etc.)

// over-ride the pure virtual function from the base:
void Log(const int& id, const int& value)
{
std::cout << "LOG OFF" << std::endl;
}
private:
// etc.
};
Research "polymorphism", "pure virtual function", and
"abstract base class" in a good C++ book for more info.

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 11 '06 #2

P: n/a

"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)
{
}
The simplest solution: use a bool flag:

void MyClass::Log(const int& id, const int& value)
{
if (logging_enabled) {
LogON(...);
} else {
LogOFF(); // or just do not call anything
}
}

/Pavel
Jul 11 '06 #3

P: n/a
cesco <fd**********@gmail.comwrote:
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?
You could use a pointer to member function, but then LogOFF() needs to
take the same number and types of parameters as LogON(), and it makes
the syntax a little nasty:
#include <iostream>

class MyClass {
public:
MyClass(bool log);
void (MyClass::*Log)(const int& id, const int& value);
private:
void LogON(const int& id, const int& value);
void LogOFF(const int& id, const int& value);
};
MyClass::MyClass(bool log)
: Log(log ? &MyClass::LogON : &MyClass::LogOFF)
{ }
void MyClass::LogON(const int& id, const int& value)
{
std::cout << "Log ON\n";
}
void MyClass::LogOFF(const int& id, const int& value)
{
std::cout << "Log OFF\n";
}
int main()
{
const int id = 0;
const int value = 42;

MyClass on(true);
MyClass off(false);

// This syntax is nasty, but it does make sense to me
(on.*on.Log)(id, value);
(off.*off.Log)(id, value);
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.