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 

How to do this??? (function pointer to other class)

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





PostPosted: Fri Jun 27, 2003 12:30 am    Post subject: How to do this??? (function pointer to other class) Reply with quote



class ABC {
public:
void abcFunc(void) { }
};

class DEF {
public:
void (ABC::*fabc)(void);
DEF()
{
fabc = &ABC::abcFunc;
(this->*fabc)(); // Fails on a namespace issue....
}
};

=============
I know I'm being elemental... but how do you do this?
I just want a function pointer to member of a different class...
Thanks!
Todd.

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





PostPosted: Fri Jun 27, 2003 2:02 pm    Post subject: Re: How to do this??? (function pointer to other class) Reply with quote



[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote in message news:<c7aadd5c.0306261247.4de8d3e2 (AT) posting (DOT) google.com>...
Quote:
class ABC {
public:
void abcFunc(void) { }
};

class DEF {
public:
void (ABC::*fabc)(void);
DEF()
{
fabc = &ABC::abcFunc;
(this->*fabc)(); // Fails on a namespace issue....
}
};

=============
I know I'm being elemental... but how do you do this?
I just want a function pointer to member of a different class...

You have a function pointer to a member of ABC. However, you need a
pointer/reference to ABC, in order to be able to use it (in your
class, 'this' is a pointer to DEF).

Do this, and it will work:
ABC a;
(a.*fabc)();

Best,
John

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

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Jun 27, 2003 6:07 pm    Post subject: Re: How to do this??? (function pointer to other class) Reply with quote



[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote in message
news:<c7aadd5c.0306261247.4de8d3e2 (AT) posting (DOT) google.com>...

Quote:
class ABC {
public:
void abcFunc(void) { }
};

class DEF {
public:
void (ABC::*fabc)(void);
DEF()
{
fabc = &ABC::abcFunc;
(this->*fabc)(); // Fails on a namespace issue....

More than just a namespace issue, it seems to me.

Quote:
}
};

=============

I know I'm being elemental... but how do you do this?

How do you do what? What you are apparently trying to do is call a
function of one class on an object of a completely different class. I
don't see any way that this could make sense.

Quote:
I just want a function pointer to member of a different class...

You've got a pointer to a function in a different class. To call such a
function, you need an object of that class. If you've got such an
object, there is no problem. If you don't, you can't call the function.

In your case, how would you call ABC::abcFunc without passing through a
pointer to member function?

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45 16

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

Back to top
Daniel Spangenberg
Guest





PostPosted: Fri Jun 27, 2003 6:14 pm    Post subject: Re: How to do this??? (function pointer to other class) Reply with quote

Good Morning, Todd!

[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] schrieb:

Quote:
class ABC {
public:
void abcFunc(void) { }
};

class DEF {
public:
void (ABC::*fabc)(void);
DEF()
{
fabc = &ABC::abcFunc;
(this->*fabc)(); // Fails on a namespace issue....
}
};


You should be aware, that a pointer of a member function, like fabc in
your example specifies a class name (here ABC) and thus has to be called

by either a reference or a pointer to such a class or one of its
accessibly
derived classes. So, either you have to derive DEF from ABC or The DEF
class should contain a pointer/reference to an ABC object or one of its
publicly
derived classes.

Yours,

Daniel Spangenberg




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

Back to top
Andrey Tarasevich
Guest





PostPosted: Sat Jun 28, 2003 12:29 pm    Post subject: Re: How to do this??? (function pointer to other class) Reply with quote

[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
Quote:
class ABC {
public:
void abcFunc(void) { }
};

class DEF {
public:
void (ABC::*fabc)(void);
DEF()
{
fabc = &ABC::abcFunc;
(this->*fabc)(); // Fails on a namespace issue....
}
};

I know I'm being elemental... but how do you do this?

You can't do this. You can't invoke 'ABC's member function with an
object of type 'DEF' since these types are completely unrelated.

Quote:
I just want a function pointer to member of a different class...

You already have a function pointer to a member of a different class.
What you seem to want is to invoke this function with an object of
completely unrelated class. This does not make any sense. Explain in
more detail why you need this.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


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

Back to top
Big Brian
Guest





PostPosted: Thu Jul 10, 2003 4:18 am    Post subject: Re: How to do this??? (function pointer to other class) Reply with quote

Quote:
class ABC {
public:
void abcFunc(void) { }
};

class DEF {
public:
void (ABC::*fabc)(void);
DEF()
{
fabc = &ABC::abcFunc;
(this->*fabc)(); // Fails on a namespace issue....
}
};

I know I'm being elemental... but how do you do this?

You can't do this. You can't invoke 'ABC's member function with an
object of type 'DEF' since these types are completely unrelated.

I just want a function pointer to member of a different class...

You already have a function pointer to a member of a different class.
What you seem to want is to invoke this function with an object of
completely unrelated class. This does not make any sense. Explain in
more detail why you need this.

Your code has a few problems.

First, if you want to pass a pointer to a member function, that member
function must be declared as static.
Second, (i believe, although it works in this case with g++) you
should not use "this" in the constructor for class DEF because inside
the constructor, the object isn't defined yet.

Try this....

#include <iostream>
using namespace std;

class ABC {
public:
static void abcFunc(void);
};


void ABC::abcFunc(void)
{
cout << "in abcFunc" < }

class DEF {
public:
void (*fabc)(void);

DEF(): fabc(&(ABC::abcFunc))
{
(fabc)();
}
};

int main(int, char**)
{
DEF c;
}


when compiled with g++ 3.1, execution prints "in abcFunc"

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

Back to top
Richard Smith
Guest





PostPosted: Fri Jul 11, 2003 1:33 am    Post subject: Re: How to do this??? (function pointer to other class) Reply with quote

Big Brian wrote:

Quote:
First, if you want to pass a pointer to a member function, that member
function must be declared as static.

Not "must". Not even necessarily "should". It depends
what's wanted. Maybe a static function will do the desired
job, in which case your code will work fine. If not a
member function pointer is perhaps needed.

The original code given perfectly sucessfully passed around
a pointer to a member function. The problme was in calling
the function: it should have been called on an object of
type ABC not DEF. So perhaps the following would be
appropriate:

class DEF {
public:
ABC *abc;
void (ABC::*fabc)(void);

DEF()
{
abc = new ABC;
fabc = &ABC::abcFunc;
(abc->*fabc)();
}
};

(Obviously care should be taken to ensure abc gets deleted
correctly -- I would use a smart pointer to do this.)

Quote:
Second, (i believe, although it works in this case with g++) you
should not use "this" in the constructor for class DEF because inside
the constructor, the object isn't defined yet.

Nonsense. You can use "this" perfectly happily in a
constructor, just be careful that (a) you're not using any
members that are not fully initialised, and (b) if you're
calling virtual functions you know exactly what you're
doing.

--
Richard Smith

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

Back to top
Big Brian
Guest





PostPosted: Fri Jul 11, 2003 7:30 am    Post subject: Re: How to do this??? (function pointer to other class) Reply with quote

Quote:
First, if you want to pass a pointer to a member function, that member
function must be declared as static.

Wrong.

No, this is not wrong. If you want to take a pointer to a member
function and be able to use it in another class as in the original
post, the member function must be static. If it's not static, it
doesn't even exist in memory because, in this example, there isn't
even an instance of ABC, so how can you get the address of abcFunc()?

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

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Sat Jul 12, 2003 8:00 am    Post subject: Defect report [basic.life], cannot call base class functions Reply with quote

Richard Smith <richard (AT) ex-parrot (DOT) com> wrote


[I've cross posted to comp.std.c++, because IMHO, this is a standards
problem, probably a defect. I'd set follow-ups as well, but I'm posting
through Google, which doesn't allow it.]

Quote:
Nonsense. You can use "this" perfectly happily in a constructor, just
be careful that (a) you're not using any members that are not fully
initialised, and (b) if you're calling virtual functions you know
exactly what you're doing.

In practice, and I think in intent, you are right. However, the
standard makes some pretty stringent restrictions in 3.8. To start
with, it says (in paragraph 1):

The lifetime of an object is a runtime property of the object. The
lifetime of an object of type T begins when:

-- storage with the proper alignment and size for type T is
obtained, and

-- if T is a class type with a non-trivial constructor, the
constructor calls has COMPLETED.

The lifetime of an object of type T ends when:

-- if T is a class type with a non-trivial destructor, the
destructor call STARTS, or

-- the storage which the object occupies is reused or released.

(Emphisis added.) Then when we get down to paragraph 5, it says:

Before the lifetime of an object has started but after the storage
which the object will occupy has been allocated [which sounds to me
like it would include in the constructor, given the text above] or,
after the lifetime of an object has ended and before the storage
which the object occupied is reused or released, any pointer that
refers to the storage location where the object will be or was
located may be used but only in limited ways. [...] If the object
will be or was of a non-POD class type, the program has undefined
behavior if:

[...]

-- the pointer is implicitly converted to a pointer to a base class
type, or [...]

I can't find any exceptions for the this pointer.

Note that calling a non-static function in the base class, or even
constructing the base class in initializer list, involves an implicit
conversion of this to a pointer to the base class. Thus undefined
behavior. I'm sure that this wasn't the intent, but it would seem to be
what this paragraph is saying.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

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

[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
------------ And now a word from our sponsor ---------------------
For a secure high performance FTP using SSL/TLS encryption
upgrade to SurgeFTP
---- See http://netwinsite.com/sponsor/sponsor_surgeftp.htm ----

Back to top
Michael
Guest





PostPosted: Sat Jul 12, 2003 1:28 pm    Post subject: Re: How to do this??? (function pointer to other class) Reply with quote

On Fri, 11 Jul 2003 03:30:09 -0400, Big Brian wrote:

Quote:
No, this is not wrong. If you want to take a pointer to a member
function and be able to use it in another class as in the original
post,
the member function must be static. If it's not static, it doesn't
even
exist in memory because, in this example, there isn't even an instance
of ABC, so how can you get the address of abcFunc()?

that's partly correct: you allways need an object if you want to call a
member function. But that doesn't mean, that the method must be static.
You can pass both, the object-pointer and the pointer to the
member-function to the other class and call ist then: (obj->*fct_ptr)()
See the examples in

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

cheers
Michael


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