Re: Member Functions

From: Vince Morgan (vinharUNSPAM_at_optusnetUNSPAM.com.au)
Date: 03/28/05


Date: Mon, 28 Mar 2005 16:44:57 +1000

Hi All,
            It is at grave risk to my dignity that I ask the following,
so please be kind : )
 I know that classes\struct have their data members
grouped together in memory, however my question relates
to their member functions.
If I create code as follows I know that I can have a number
of instances of struct S and that each instance of this struct
will have a pointer to a single function.
If I were to implement this as a class with a member function
instead of the function pointer, would that member function
exist in a similar fashion, or would the function code be
replicated in every instance of the class?

#include <iostream>
#include <ostream>

int Func(int i)
{
 return i + 42;
}

struct S
{
 int a;
 int (*pFunc) (int);
 S(): pFunc(&Func), a(0) {}
};

int main()
{
 S s[10];
 s[0].a = 3;
 int b = s[0].pFunc(s[0].a);
 cout << b << endl;

 return 0;
}

TIA
Vince Morgan



Relevant Pages

  • Re: function wont work inside MFC dialog class
    ... int __stdcall gotSensor(CPhidgetInterfaceKitHandle phid, void *meh, int ind, int val); ... Declaring your member function gotSensor() as static, if possible, will correct this problem. ...
    (microsoft.public.vc.mfc)
  • Re: function wont work inside MFC dialog class
    ... int __stdcall gotSensor(CPhidgetInterfaceKitHandle phid, void *meh, int ind, int val); ... Declaring your member function gotSensor() as static, if possible, will correct this problem. ...
    (microsoft.public.vc.mfc)
  • Re: function wont work inside MFC dialog class
    ... int __stdcall gotSensor(CPhidgetInterfaceKitHandle phid, void *meh, int ind, int val); ... Declaring your member function gotSensor() as static, if possible, will correct this problem. ...
    (microsoft.public.vc.mfc)
  • Re: [C++] Help Parsing an Integer/Character Mix
    ... - What kind of data structure are you using? ... int Number; ... while(InFile>> Buffer) ... possible even an member function. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Non-static member function as a callback
    ... > struct A { ... > int f; ... a member function pointer is not a simple function ...
    (microsoft.public.vc.language)