473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

dynamic function definition

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
3 6324
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tito | last post by:
From the Python's tutorial, about default argument values: <quote> The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list,...
4
by: jack | last post by:
Hi there, I have a short question about template function definition. 1) The following files will not compile in M$ VC++ but will have no problem with g++. 2) However, if I uncomment all...
1
by: Toby Miller | last post by:
I have this form validation that I'm trying to build, but it's not working properly. A dynamic function to the onsubmit event for a form. the result of that function (true/false) should then be...
1
by: rajesh6695 | last post by:
Hai Where the pre defined function definitions and declarations will be available either in the header file or in the library file... I think it will be in the library files i am not sure so...
11
by: djhong | last post by:
Hi, BSD code has following hcreate function definition in hsearch.c under lib/libc/db/hash/hsearch.c. Here, why is 'extern' used ? What's the purpose of it? How come in infront of...
13
by: Old Wolf | last post by:
I have some code that has in the header file: void foo( char bar ); and in the source file: void foo( bar ) char bar; { /* etc. */ } The compiler (with many warnings enabled) warns that...
3
by: MarQade | last post by:
Hi, I am new here. I am also a first-year computer science student looking for some help with a very small C++ program. This is a 3-file program, header file with declarations and 2 source files,...
5
by: aryan | last post by:
Is it not allowed to have template function definition in a .cc file? Here is the scenario. The tempage function declaration is in a header file and the definition in a .cc file. The function is...
10
by: JRough | last post by:
I have a function definition error: Warning: Missing argument 2 for makexcldata(), called on line 123 and defined in on line 149 "Warning: mysql_fetch_row(): supplied argument is not a valid...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.