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 

Pointer to member function - can it be done without using st

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





PostPosted: Wed Jan 14, 2004 11:02 pm    Post subject: Pointer to member function - can it be done without using st Reply with quote



I have class that contains a member function that is to be implemented
as a task. This task is a 'receive message' type of task that will
pend until receipt of a message. It determines the message type and
then call the appropriate function in the appropriate class to process
the message.

I need to get a pointer to this member function but do not want to
have to declare my pointer as static. Actually, if someone can help me
out here with an understand of what that would mean relative to my
example. I understand what static means but books have eluded to the
fact that data that a static function operates on needs to be static
also. What if that operation calls another operation in another class.
Does that other operation have to be static?

Is it enough that if I only ever have one instance of this task
running, just don't let it modify any data members in the class
directly. Use get/set routines, or would those have to be static too.
Does that make sense?

Thanks so much in advance.

(PS: In case it isn't totally obvious, I am new to C++. As such, I am
somewhat intimidated by some C++ syntax. No offense will be taken if
you reply with simple syntax and, if possible, comment well! :-)

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





PostPosted: Thu Jan 15, 2004 10:32 am    Post subject: Re: Pointer to member function - can it be done without usin Reply with quote



Hi!

Andrea wrote:
Quote:
I understand what static means but books have eluded to the
fact that data that a static function operates on needs to be static
also. What if that operation calls another operation in another class.
Does that other operation have to be static?

Look at this:

struct Foo
{
static int i;
int j;
static void setFoosI(int i_)
{
i = i_;
//j = i_; //not allowed, j is not static
}

static void setFoosJ(Foo *this_, int j_)
{
i = j_; //is allowed
this_->j = j_; //j is not static, but we have an object
}

//this last method is the whole idea of member functions:
//you can rewrite the above as:
void setJ(int j_)
{
i = j_; //is allowed
this->j = j_;
}
};

So if you have an object (or pointer to an object), then you can access
non static data even from static methods.

Frank


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

Back to top
Daniel Krügler (nee Spang
Guest





PostPosted: Thu Jan 15, 2004 3:08 pm    Post subject: Re: Pointer to member function - can it be done without usin Reply with quote



Good morning, Andrea!

Andrea schrieb:

[snip]

Quote:
I need to get a pointer to this member function but do not want to
have to declare my pointer as static. Actually, if someone can help me
out here with an understand of what that would mean relative to my
example. I understand what static means but books have eluded to the
fact that data that a static function operates on needs to be static
also. What if that operation calls another operation in another class.
Does that other operation have to be static?

The problem is, that static has several meaning, which depend on the
context, the corresponding keyword is used in. The sentence

"[..] but books have eluded to the fact that data that a static function
operates
on needs to be static also."

is plainly wrong, independent on the meaning of static. What comes next to
that
description is a static member function, which has no access to the this
pointer,
so it can use only its arguments and globally accessible objects or
functions.

There are no operational limitations of the static member function
static_foo and
the non-static member function foo in the example below:

class A
{
void foo() const; // Non-static member function
static void static_foo(const A& local_this); // Static member function
};

because static_foo can use its argument local_this similar to non-static
member functions (implicitely) using the this pointer. Differences come
into play, if you consider virtuality of functions and the type of
function
pointers here, but you have the possibility to get either a "normal"
function
to a static member function or a member function pointer to a non-static
member function.

Quote:
Is it enough that if I only ever have one instance of this task
running, just don't let it modify any data members in the class
directly. Use get/set routines, or would those have to be static too.
Does that make sense?

I have difficulties understanding of your meaning of a "static pointer"
mentioned above. Do you mean a static data member, which is a pointer
in this case to reach a singleton-like behaviour? (Singletons are objects
which exists exacly once in a program, in a thread, in a .. whatever).
If you use such a singleton, you can call its member functions and that's
it. In this case the singleton object saves the complete state its member
functions act on.

Quote:
(PS: In case it isn't totally obvious, I am new to C++. As such, I am
somewhat intimidated by some C++ syntax. No offense will be taken if
you reply with simple syntax and, if possible, comment well! Smile

Each of us has once started learning C++, so I think that everyone should
understand your position. Unfortunately, I am missing somewhat more
information to understand your actual problem...

If you can be somewhat more precise concerning your requirements I
am sure, I can provide somemore useful advice.

Greetings from Bremen,

Daniel




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

Back to top
Robert Kindred
Guest





PostPosted: Fri Jan 16, 2004 2:50 am    Post subject: Re: Pointer to member function - can it be done without usin Reply with quote


"Andrea" <forwork (AT) comcast (DOT) net> wrote

Quote:
I have class that contains a member function that is to be implemented
as a task. This task is a 'receive message' type of task that will
pend until receipt of a message. It determines the message type and
then call the appropriate function in the appropriate class to process
the message.


I can't tell for sure, but it sounds like you are using your operating
system to
'activate' a member function as a thread. This member function must be
static,
because the operating system makes a C-style call to your function, which
will
not have the invisible 'this' parameter in the call. To get the 'this' in
there anyway
depends on which operating system you are using.

Quote:
I need to get a pointer to this member function but do not want to
have to declare my pointer as static. Actually, if someone can help me
out here with an understand of what that would mean relative to my
example. I understand what static means but books have eluded to the
fact that data that a static function operates on needs to be static
also. What if that operation calls another operation in another class.
Does that other operation have to be static?


It is really nice having a task or a thread treated as an instantiated class
in your program.
The ACE libraries pull this off. You might look in their source to see how
they do it.

<snip>
Quote:

Thanks so much in advance.

hth,


Robert Kindred


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

Back to top
Ben Hutchings
Guest





PostPosted: Fri Jan 16, 2004 2:57 am    Post subject: Re: Pointer to member function - can it be done without usin Reply with quote

Andrea wrote:
Quote:
I have class that contains a member function that is to be implemented
as a task.

Do you mean that it will be the main function of a thread?

Quote:
This task is a 'receive message' type of task that will
pend until receipt of a message. It determines the message type and
then call the appropriate function in the appropriate class to process
the message.

I need to get a pointer to this member function but do not want to
have to declare my pointer as static.

Supposing the thread API has this thread creation function:

handle start_thread(int (* func)(void *), void * arg);

then you can have it call your non-static member function using a
wrapper function:

class message_processor
{
void start()
{
thread_handle_ = start_thread(run_wrapper, this);
if (!thread_handle_)
throw ...;
}

// Wrapper function - does the necessary pointer conversion
// then calls the required function.
static int run_wrapper(void * obj)
{
return static_cast<foo *>(obj)->run();
}

// The function you're really interested in.
int run();

handle thread_handle_;

...
};

If the thread creation function doesn't take an argument that it
passes into the thread function, then it's broken!

Quote:
Actually, if someone can help me
out here with an understand of what that would mean relative to my
example. I understand what static means but books have eluded to the
fact that data that a static function operates on needs to be static
also.

A static member function does not operate on an instance of its
class (it does not have an implicit "this" parameter) so there are
no non-static member variables for it.

Quote:
What if that operation calls another operation in another class.
Does that other operation have to be static?

No. If it can get a pointer to a class instance from a static
variable, an explicit parameter or "new" then it can use that to
call non-static functions.

Quote:
Is it enough that if I only ever have one instance of this task
running, just don't let it modify any data members in the class
directly. Use get/set routines, or would those have to be static too.
Does that make sense?
snip


If the thread creation function doesn't take the extra argument,
and if only one instance needs to run, then you can use a static
variable in place of the function argument:

class message_processor
{
void start()
{
thread_handle_ = start_thread(run_wrapper);
if (!thread_handle_)
throw ...;
}

static int run_wrapper()
{
return instance_->run();
}

int run();

handle thread_handle_;

// The class will be a singleton, and this will point
// to its instance.
static message_processor * instance_;

...
};

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

Back to top
Andrea
Guest





PostPosted: Fri Jan 16, 2004 2:59 am    Post subject: Re: Pointer to member function - can it be done without usin Reply with quote

Thanks Frank for answering so quickly. But, either I mis-stated my
problem or I'm not understanding your solution.
This isn't complete but an idea of what I'm trying to do: (BTW, we're
using Greenhills/Integrity)
----------
class Message_Receiver
{
public:
Message_Receiver();
~Message_Receiver();
void message_receiver_task();
}
----------
void Message_Receiver::message_receiver_task()
{
while (true)
{
message = wait_for_message
switch (message.ID)
{
case 1:
Case1MessageClass::processCase1Message();
break;
case 2:
Case2MessageClass::processCase2Message();
break;
Back to top
Xenos
Guest





PostPosted: Fri Jan 16, 2004 1:03 pm    Post subject: Re: Pointer to member function - can it be done without usin Reply with quote


"Andrea" <forwork (AT) comcast (DOT) net> wrote

Quote:
I have class that contains a member function that is to be implemented
as a task. This task is a 'receive message' type of task that will
pend until receipt of a message. It determines the message type and
then call the appropriate function in the appropriate class to process
the message.

I need to get a pointer to this member function but do not want to
have to declare my pointer as static. Actually, if someone can help me
out here with an understand of what that would mean relative to my
example. I understand what static means but books have eluded to the
fact that data that a static function operates on needs to be static
also. What if that operation calls another operation in another class.
Does that other operation have to be static?

Is it enough that if I only ever have one instance of this task
running, just don't let it modify any data members in the class
directly. Use get/set routines, or would those have to be static too.
Does that make sense?



If the function your particular system uses to spawn the task allows it to
give the task parameters, then you are in luck. You can create a static
wrapper function that takes the this pointer as a parameter and use it to
call the actual function member. In this case, what I usually do is define
an abstract task class like such:

class task {
public:
// whatever ctors that are appriopriate
virtual ~task();
void start(); // uses OS specific call to spawn task via wrapper,
passing it the this pointer
virtual func() = 0; // actual task function, override to provide
functionality
private:
static void wrapper(task* p) {p->func();}// actual function spawned by
OS
};

For each task you need, define a child class of task and override func. You
may have to use different parameter type for the wrapper depending on your
system (i.e., a lot of systems only pass integers to the task function, so
you will have to make use of reinterpret_cast.

You will also have to think about such things as whether the destuctor
should terminate the task (including if it has already died or is
unresponsive), protecting the object from copy construction and assignment,
etc. It is also important NOT to spawn the task from the constructor,
because the task may start execution and possibly use class data before
construction is complete in the case of childern of class task!

Hope this helps,

DrX






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

Back to top
Thomas Mang
Guest





PostPosted: Fri Jan 16, 2004 6:07 pm    Post subject: Re: Pointer to member function - can it be done without usin Reply with quote



Andrea schrieb:

Quote:

I need to have a pointer to my task (pointer to member function
message_receiver_task). According to "Effective C++", pointers to
member functions need to be declared as static.

Where did you read this (Chapter / Item)?
Maybe this is a cause of confusion?


regards,

Thomas

[ 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.