Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Frasncis Glassboro wrote.
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Garrett Hartshaw  
View profile  
 More options Jan 7 2011, 5:47 pm
Newsgroups: alt.comp.lang.learn.c-c++, comp.lang.c++
From: Garrett Hartshaw <gharts...@gmail.com>
Date: Fri, 07 Jan 2011 17:47:12 -0500
Local: Fri, Jan 7 2011 5:47 pm
Subject: Re: Frasncis Glassboro wrote.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/07/2011 02:06 PM, Paul wrote:

> "James Kanze" <james.ka...@gmail.com> wrote in message
> news:7148a492-1586-4421-bc6c-4afa38770f53@w29g2000vba.googlegroups.com...
>> On Jan 7, 2:07 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
>>> "James Kanze" <james.ka...@gmail.com> wrote in message

>>    [...]
>>> >> I can understand the concept you express but
>>> >> a) how do you get the address of a member function?

>>> > &ClassName::functionName

>>> > Concretely:

>>> >    struct C { void f(); };
>>> >    void (C::*pf)() = &C::f;

>>> >> b) what happens if this member function is virtual?

>>> > It works correctly.  That's why pointer to member functions are
>>> > often larger than any other pointer types (but there are other
>>> > ways of solving the problem).

>>> What would your pointer point to ?

>> That's the compiler writers problem, not mine:-).

> It certainly is a problem for the compiler, and perhaps the program too.
> Especially if you didn't initialised the empty pointer.
> Let me put it another way, where would you get the address for the
> virtual function?

>>> You cannot do this with virtual functions and  you are wrong
>>> to suggest it works correctly.

>> It does work, and I've done it.  More than once.

> It simply can't be done as the concept of virtual functions only lives
> in the world of objects.
> Please show some basic code. I guarantee you cannot.

#include <iostream>

class C1 {
public:
    virtual void f() {
        std::cout << "C1::f" << std::endl;
    }

};

class C2 : public C1 {
public:
    virtual void f() {
        std::cout << "C2::f" << std::endl;
    }

};

int main () {
  void (C1::*p)() = &C1::f; //create a pointer to a member function

  C1 a;                         //create a object of type C1
  C1 * b;                       //create a object of type pointer to C1
  b = new C2();                 //allocate a object of type C2

  (a.*p)();                     //call the member function pointed to
                                //by p, with &a as this
  (b->*p)();                    //call the member function pointed to
                                //by p (virtually), with b as this

  return 0;

}

This code prints the following.

C1::f
C2::f
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJNJ5fwAAoJEO0SzzQOdchN7x0P/j9jO/ptm/i8ir1LzUI9WhpN
P1n/A3rjYALl3i6oj/+MzE91+yuWcPgA0raXHnHITUDldfg9IdDXq0x8M/hDupWG
nrvGCOEmkXmHWn6cw6lMgGS9TlGtyvWcR9wcDAAC2Zq/b6NcctSWKWbWhSOiJ5Im
zC3mx0b4n+lh22S78nCHLy+9Nsx2+3Czmrwjo5D6X4kI4uUQ837NwMuEIf0w5Wlt
rDCxg0/694yukocZomnaOveZJNt8GXVcwrQQEdI4o87CVGTKWLLNLzXN8F8Aa7vr
QpdZrL8U7PyTBofQNoHUzFJDuIkBno7zWGNR7Y3Ql9yu8fhx9MHubTvv4mfUrTab
gM5vj2HmLI00EflOhpczx1EN9McBvWOga+CPoetzf0ovauO/n2aOjhrAkeGMYKZY
dmVmrSyhXnCaGw2SfhWduRn2aY/yMOUdEoT28/MFI1ruNWj6DKyOtvJiKm3MB3Za
+5u5yO/XH2+CZIpXhCEQrYi6wwIAzv1pfMGhUV2tylO/ZSiP0toItZr3vm7JuBEK
8TSs+KvmLeJ+97XmThfPoPWMThHfLXmLqy/3WasmvDkTdiIrI1brf0gugQuAugHw
xjfcfzrRGtm6yaL8CSlAlqb/vmJiafi8m6Qjju1WuJTtaS+YQGAP5gAdQ2a2W3/S
FbMuQtdtJm6baWiB455Z
=u89w
-----END PGP SIGNATURE-----


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.