On 9 mai, 19:56, Tim Frink <plfr...@[EMAIL PROTECTED]
> wrote:
> > Probably you did print "true" because the memberfunction is
> > not null.
> But why did I not get the address? I though that &A::printA is
> a pointer. So, I can't understand why I get "true".
It's not a pointer, it's a pointer to member. Not the same
thing.
> > Apart from that, I do not see what interest you could have in getting
> > its adress. If it is out of curiosity, I would recommend you to
> > examine the value in a debugger instead.
> This was just for curiosity. When this value is passed as an
> argument, it would make sense to check if it's not 0. So I think
> that the check of the address makes sense in some situations.
It can make sense to check whether it is a null pointer. The
integral constant expression 0 will convert to a null pointer to
member in appropriate contexts. None of which has anything to
do with "address"; a pointer to member is never an "address",
just something which the compiler knows how to use in the
appropriate context.
> > I am not sure I can decipher the above, but if your
> > understanding is that a memberfunction somehow is placed
> > inside an object, your understanding is wrong. A
> > memberfunction is does not take up any space in an object.
> I'm not aware of the code generation for C++ programs. But
> from your post I assume that for each C++ class exactly one
> code (fragment) is allocated in the object's text section
> independently if and how many times objects of this class are
> instantiated.
Exactly. The executed code is the same for all instances, so
can be treated as if it were static, and it's not modifiable, so
it can be treated as a const.
> So what happens when an object of a particular class is
> instantiated? Will additional code be allocated in the .text
> section?
Why? The code is the same for all instances.
> Or will this new object be considered as data allocated on the
> stack/heap with some references to the class code located
> somewhere in the .text section?
No. The only reference to the instance is the this pointer,
passed as an additional argument when calling a member function.
> >> And this brings me to my second question concerning line
> >> 13. When I try to compiler this code, I get the compiler
> >> error: error: must use .* or ->* to call pointer-to-member
> >> function in `func (...)'
> > (repeating offensive code)
> >> 11 void B::printB( void (A::*func)(void) )
> >> 12 {
> >> 13 *func(); // not working
> >> 14 }
> >> 15
> > This is because your perception of a member-function pointer
> > is wrong. A member-function pointer is a pointer that can
> > be used to call a member function oon all objects of the
> > given type. But you have to provide an object. So your code
> > should have been somewhat like:
> So, why do I need the object and can't access the function (in
> my case func) without the object?
Because there's no syntax which sup****ts it? Define first what
you mean by "access the function". You can call the function on
an object of an appropriate type, and you can compare the
pointer to member with a null pointer constant or with other
pointers to members.
> If it's already in the memory (.text section) and func has the
> address to it, so why is the object still needed for the
> access?
Because you need to initialize the this pointer in order to call
the function. You can't call a member function without an
object. Either directly, or through a pointer to member
function.
> My assumption would be that an instantiated object has some
> references which point to addresses (or functions/attributes
> behind theses addresses) that is is allowed to access. So, the
> object code contains a sort of permissions about what code can
> be accessed and which not. Are these thoughts going into the
> right direction?
The implementation is somewhat complicated by the presence of
virtual functions. In practice, a non-virtual member function
is just a regular function with an additional, invisible
parameter: the compiler p***** the address of the object as
argument to this parameter, which becomes the this pointer in
the function. Virtual functions are more complicated; if the
class contains one or more virtual functions, the compiler will
typically allocate an additional hidden pointer in the class
member, and initialize it in the contructor to point to a table
with the addresses of the functions for that class.
A pointer to member function has to take this into account.
Consider something like:
struct Base
{
virtual void f() ;
} ;
struct Derived : Base
{
virtual void f() ;
} ;
void (Base::*pfm)() =3D &Base::f ;
Base* pObj =3D new Derived ;
(pObj->*pfm)() ; // calls Derived::f
--
James Kanze (GABI Software) email:james.kanze@[EMAIL PROTECTED]
en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34


|