About functino pointer to virtual functions of a class

P

parag

hi All
Is it possible to get the the function pointer of a virtual function
of a class .More so, what could be the use for the same,
Functors typically use Class method address , but for vritual
functions ,how will it access it without the objects address.

Internally all class methods are rewritten to magled names with the
this pointer as its first argument

Class A{
void print();};

will become
A_print(A* this ) ;

But for virtual functions what will be the way to do so
 
A

Alf P. Steinbach

* parag, on 21.05.2010 17:33:
Is it possible to get the the function pointer of a virtual function
of a class .

Assuming you mean a pointer to an ordinary (non member) function, the underlying
implementation, yes anything is possible, and before the standardization of C++
compilers used to support that

Now they generally don't, so you'd have delve into assembly language to do that.

However, in standard C++ you can obtain member pointer, which isn't really in a
pointer but more like an offset: you need to supply it with an object to use it.

More so, what could be the use for the same,

That sounds like homework, sorry.

See the FAQ about homework questions.

Functors typically use Class method address ,
but for vritual
functions ,how will it access it without the objects address.
Huh.



Internally all class methods are rewritten to magled names with the
this pointer as its first argument

Class A{
void print();};

will become
A_print(A* this ) ;

No. It is a valid to think about it, though, as long as you keep in mind that
the compiler can do whatever it wants as long as it produces the right behavior.
With most compilers the 'this' argument is passed differently from other
arguments, as an optimization, and name mangling is compiler-specific.

But for virtual functions what will be the way to do so

Huh?

But perhaps your question is answered by googling for 'vtable'.

Keep in mind that although that's how things work in practice, other
implementation techniques are possible, and have been used of old.


Cheers & hth.,

- Alf
 
P

parag

* parag, on 21.05.2010 17:33:




Assuming you mean a pointer to an ordinary (non member) function, the underlying
implementation, yes anything is possible, and before the standardization of C++
compilers used to support that

Now they generally don't, so you'd have delve into assembly language to do that.

However, in standard C++ you can obtain member pointer, which isn't really in a
pointer but more like an offset: you need to supply it with an object to use it.


That sounds like homework, sorry.

See the FAQ about homework questions.




No. It is a valid to think about it, though, as long as you keep in mind that
the compiler can do whatever it wants as long as it produces the right behavior.
With most compilers the 'this' argument is passed differently from other
arguments, as an optimization, and name mangling is compiler-specific.


Huh?

But perhaps your question is answered by googling for 'vtable'.

Keep in mind that although that's how things work in practice, other
implementation techniques are possible, and have been used of old.

Cheers & hth.,

- Alf

Thanks a lot. I should have gone through some more literature before
coming here
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top