Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function pointer problem as an event

Status
Not open for further replies.

EngineersForge

Technical User
Aug 26, 2003
15
CA
First of all, thank u all for the help so far! It is very quick and right on the money.

Now, I wish to use a function pointer as an event raised by a class type inherited by another. This is in a basic console, single thread for now as I am just testing the class hierarchy.

I have one public

void (*fpRaiseEvent) (void);

function pointer that is called also in a public fucntion. The main program defines the function to just print out some text. When I invoke the method from an instance of the class everything is ok. Now when I copy the class by another class object things go south some where in the copy constructor. This is my code:

sinc. func. pointers:
*fpReqSer = *rhs.fpReqSer; //complies but does not work
assign func. pointer:
void (*fpReqSer) (void) = rhs.fpReqSer; // complies but does not work
also complies:
fpReqSer = rhs.fpReqSer; //does not work

Their are many other combinations haha, all of which cause an access violation at run time. The more I stare at the pointer arithmetic, the less I know??? How does one assign the ‘being copied object’s’ pointer already assigned, to also let the coping object point to the same function?

Tanx
 
I'm not sure I understood well you, but there is a short sample of using pointers to global functions and to class members functions:
void xxx()
{
cout<< &quot;xxx&quot;<< endl;
}
class aaa
{
int x;
public:
aaa(int _x): x(_x){}
void xxx()
{
cout<< &quot;aaa:xxx>&quot;<< x << endl;
}
};
class bbb: public aaa
{
};

int main()
{
void (*ss)();
ss = xxx;
ss();
void (aaa::*sa)();
sa = &aaa::xxx;
aaa a(123);
(a.*sa)();
return 0;
}


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
this one is with inheritance:
class aaa
{
int x;
public:
aaa(){}
aaa(int _x): x(_x){}
virtual void xxx()
{
cout<< &quot;aaa:xxx>&quot;<< x << endl;
}
};
class bbb: public aaa
{
int x;
public:
bbb(int _x): x(_x << 2){}
void xxx()
{
cout<< &quot;aaa:xxx>&quot;<< x << endl;
}
};

int main()
{
void (aaa::*sa)();
sa = aaa::xxx;
aaa a(123);
(a.*sa)();
bbb b(123);
(b.*sa)();

return 0;
}


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
The C++/OO way of using function ptrs is the virtual methods.

Would you perhaps consider passing a raise event interface ptr about instead of a function ptr?

Code:
class RaiseEventI
{
  public:
   virtual void Raise() = 0;
   ...
};

class RaiseSomeSpecificEvent : public RaiseEventI
{
  // Implement the Raise method
  ...
}

class Foo
{
  RaiseEventI* mRaiseEvent;
public:
  void Bar()
  {
     mRaiseEvent->Raise();
  }
}

etc


/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Ok thank you for the samples; however, I am after something else. I don't wish to use an event object until later. As for the code by IonFilipski, I have to be honest, I have no idea. To cryptic.

Here is another attempt:

calss a
{
public:
void (*fpReqSer) (void);
void RequestService(void);
};

void a::RequestService(void)
{
//raise event
fpReqSer();
}

NOW

class b: public a
{
whatever...
};

NOW

void service1(void){print some stuff out};

void main(void)
{
b Foo;
Foo.fpReqSer = &Service1;

pod.RequestService();//works just fine

b Foo1(Foo);
Foo1.RequestService();//access violation ???
}

I think the problem must be int he copy constructor impl.

HERE IT IS

a::a(const a &rhs)
{
*fpReqSer = *rhs.fpReqSer; //something not right???
}

Could it be a question of security using Visuals C++ non ANSI/ISO compilier??? I don't see why one can't copy a pointer to a function from another class type of the same kind.

tanx
 
EF,

Code:
a::a(const a &rhs)
{
  //*fpReqSer = *rhs.fpReqSer; //something not right??? yes
  // try this
  fpReqSer = rhs.fpReqSer;  
}

I had this wonderful huge explanation written out, but I lost it when I clicked on preview. If you want to know why, just post again...

-adeh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top