www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/08/11/17:28:26

From: wrh AT placer1 DOT wimsey DOT com (William Hughes)
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Internal function pointers
Reply-To: wrh AT placer1 DOT wimsey DOT com
Date: Thu, 11 Aug 94 09:51:10 PDT

Hello, everyone:

This is off-topic, but it kind of carries on from previous postings I've made (mice and interrupts), and I'm hoping some of you have the knowledge I need.

I want to have instances of a class y (which is derived from class x) be data members of class z (which is also derived from class x).  Now, this is no problem.  Djgpp lets me do this just fine.  Works good too.

The problem comes in when I want to initialize a function pointer (which is a data member of class x) in an instance of class y (a data member of z) to contain the address of a function in an instance of class z.  The function I want to point to is private to class z.

This all seems very confusing, but its not, just draw a picture.  The way that I am initializing the data member of class y (a data member of z) is by passing the address of the function in z to a function (a data member of x) that stores it for later use.  ie:

class x
{
  protected:
  void (*paction)(void *p);
  ...
  public: 
  void setaction(void (*)(void *));  // stores to paction
  ...
}

class y:x
{...}

class z:x
{
  protected:
  class y one,two,three;
  ...
  void myfunc(void *);
  void myotherfunc(void);
  ...
  public:
  ...
}

void z::myotherfunc(void)
{
  one.setaction(myfunc);  
}

Now, djgpp doesn't compile this, it complains about my prototyping.  What gives?  None of these functions are virtual (although it shouldn't matter).
I worked around by determining the times that the instances one, two, three *should* have called myfunc() and just did it within class z, but that isn't very elegant...

Note that this works fine if myfunc is not within the class, but someone else's function.  It only gripes when myfunc is part of the class.

Should this be happening, or am I just abusing the structure of c++ way, way, way too much...?  Some of these derived classes are stacked deeper than this.  If I am abusing, can I just cast it to get it to work?  (Ooh Ick)

William Hughes

ps:
  The idea behind the classes is a signal passing thing.  If an object receives the proper signal, it calls its 'action function' and executes a bit of code.  Class z is a 'meta-object' containing several other objects.  It directs the signal(s) to one or more of its sub-objects.  These objects in turn execute bits of code (if appropriate) within class z to fiddle with class z's data (z might execute its own action function too)


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019