Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > How to do this??? (function pointer to other class)
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 15th July 16:41
work
External User
 
Posts: 1
Default How to do this??? (function pointer to other class)



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! ]
  Reply With Quote


  sponsored links


2 23rd July 15:04
External User
 
Posts: 1
Default Defect report , cannot call base class functions from constructor, was Re: How to do this??? (function pointer to other class)



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

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@gabi-soft.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 ----
  Reply With Quote
3 31st July 16:02
michael
External User
 
Posts: 1
Default How to do this??? (function pointer to other class)


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! ]
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666