C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

dynamic function definition

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
cesco
Guest





PostPosted: Wed Jul 12, 2006 1:41 am    Post subject: dynamic function definition Reply with quote



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! ]
Back to top
Gerhard Menzl
Guest





PostPosted: Thu Jul 13, 2006 3:37 am    Post subject: Re: dynamic function definition Reply with quote



cesco wrote:

Quote:
I need to define the body of a function in the constructor of a class.

Impossible. You cannot define a function, member or free, in the body of
another function.

Quote:
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:

You have not revealed any details of the condition upon which the
behaviour depends. Is it decided at runtime or at compile time which
version is to be called? In the former case, does the condition change
during the lifetime of a particular MyClass object? Assuming a runtime
decision, that the condition is part of the state of MyClass, and that
MyClass::Log() works like a toggle switch, it seems that a boolean
member and a simple conditional expression would do the trick:

Quote:
class MyClass
{
public:

MyClass();

Quote:
void Log(const int& id, const int& value);
private:
void LogON(const int& id, const int& value);
void LogOFF();

bool myLogIsOn;

Quote:

};


Quote:
MyClass::MyClass

() // don't forget the parentheses
: myLogIsOn (/* put a ?: expression here if the condition is
simple enough, otherwise initalize myLogIsOn
in the body of the constructor */)

Quote:
{
}


Quote:
void MyClass::LogON(const int& id, const int& value)
{
// do something like push_back
}

void MyClass::LogOFF()
{
// do nothing
}

void MyClass::Log(const int& id, const int& value)
{

if (myLogIsOn)
{
this->LogOFF();
myLogIsOn = false;
}
else
{
this->LogON(id, value);
myLogIsOn = true;
}

Quote:
}

By the way, is there a particular reason why you pass id and value as
const references instead of by value?

Please be more specific if this should not solve your problem.

--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".

The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Thu Jul 13, 2006 3:41 am    Post subject: Re: dynamic function definition Reply with quote



cesco wrote:

Quote:
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! ]
Back to top
Greg Herlihy
Guest





PostPosted: Thu Jul 13, 2006 3:52 am    Post subject: Re: dynamic function definition Reply with quote

cesco wrote:
Quote:
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();

};

The typical C++ approach to this problem would use class inheritance
and virtual methods to implement the necessary dynamic behavior. The
only complication in this particular situation is that the dynamic
behavior of the object is not determined until the object is already
under construction - which rules out implementing the dynamic behavior
in the class of the object itself (the MyClass class in this case).

The usual way for a MyClass class object to behave dynamically after
its construction, is to implement the dynamic behavior in an
"implementation" class object that is stored as a MyClass data member.
Then MyClass effectively implements dynamic behavior by fowarding
static calls to this dynamic data member. The code below shows how this
arrangement might look in practice:

#include <cstdlib>

// declare an abstract base class for the implementation class
class LogImplementation
{
public:
virtual ~LogImplementation() {}

virtual void Log( const int& id,
const int& value) = 0;
};

// a concrete log implementation
class LogOnImplementation : public LogImplementation
{
public:
virtual void Log(const int& id, const int& value)
{
// LogON implementation goes here
}
};

class LogOffImplementation : public LogImplementation
{
public:
virtual void Log(const int& id, const int& value)
{
// LogOFF implementation goes here
}
};

// the MyClass object provides the client interface for logging
class MyClass
{
public:
MyClass();

// the Log method is forwarded to the implementation
void Log(const int& id, const int& value)
{
mImplementation->Log(id, value);
}

private:
LogImplementation *mImplementation;
};

// MyClass constructor selects its dynamic behavior
// by allocating the appropriate implementation data member
MyClass::MyClass()
: mImplementation(NULL)
{
// select the desired behavior here
mImplementation = new LogOnImplementation;
// or
mImplementation = new LogOffImplementation;
}

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
iwongu
Guest





PostPosted: Thu Jul 13, 2006 4:02 am    Post subject: Re: dynamic function definition Reply with quote

cesco =EC=9E=91=EC=84=B1:

Quote:
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

I think that you just need a member function pointer for selected log
function.

Here is the code. (It's better to use just 'int' than 'const int&'. So
I use it)

class MyClass
{
public:
MyClass(bool fOn) : PLOG_FUNC(fOn ? &MyClass::logOn :
&MyClass::logOff) {
}

void log(int id, int value) {
(this->*PLOG_FUNC)(id, value); // call selected log() function
}

private:
void logOn(int id, int value) {
cout << "LogOn\n";
}
void logOff(int , int ) {
cout << "LogOff\n";
}

private:
void (MyClass::*PLOG_FUNC)(int , int ); // member function pointer
for log()
};

Or, if you really want to define the body of log function in the
constructor, you can use the 'function' and 'lambda' library of the
'boost.'

Here is code, again.

class MyClass
{
typedef function<void (int, int)> func_t;
public:
MyClass(bool fOn)
: func_(fOn ?
func_t(var(cout) << "LogOn\n") :
func_t(var(cout) << "LogOff\n")) // you can use _1, _2 for id and
value params
{
}

void log(int id, int value) {
func_(id, value);
}

private:
func_t func_;
};

Or, you can remove dummy log function entirely like the following just
for fun :-)

class MyClass
{
typedef function<void (int, int)> func_t;
public:
MyClass(bool fOn)
: log(fOn ?
func_t(var(cout) << "LogOn\n") :
func_t(var(cout) << "LogOff\n")) {
}

const func_t log; // it should be const not to allow reset by user.
};

With this, you can use the same syntax of upper class.

MyClass on(true);
on.log(1, 2);

By the way, I can't find any harm of public member variable in this
case. It's curious. :-)

=2D---
iwongu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.