function pointers?

Started by
8 comments, last by BriarLoDeran 23 years, 10 months ago
Here''s the story old friends... Right now I''ve got a fairly intense (by my standard anyway... )class going, so to illustrate this, I will use the simple class found in exibit A:
class Pineapple
{
  public:

  int X;
  int Y;

  int Add(void) {return(X + Y);}
  int (*Zebra)();

}; 
Now I hope you have noted the function pointer Zebra(). So, now I want the function to be user-defineable such as follows:
int Sword()
{  
} 
.
.
.
Zebra = Sword; 
So this shouldn''t be problem, right? Well here is how I want to define Sword:
int Sword()
{
  return (X - Y);
} 
I guess I shouldn''t have spent all this time to illustrate such a simple question, too late now though... Anyway, I want to use the X and Y from the class, but this seems to annoy the compiler simply because it has no idea what these vaibles are...why can''t it just know?!? So, how do I go about ammending this wee problem? It seems it me that this is a very C way of doing things and there must be some appropriate way to accomplish this in C++. Anyway, I desperately await the elusive answer! Thank you for your time and such... -> Briar LoDeran <-
Advertisement
Well, the compiler won''t let you use X,Y in your Sword function because Sword() isn''t a part of your Pineapple class. To do what I think you want to do you would need to change sword to something like this:

int Sword(Pineapple* p) //
{
return (p->x - p->y);
}

Of course you would want to change your class function pointer parameter to match also. Also remember if Sword() is declared in a different header file to include the Pineapple header.

int (*Zebra)(Pineapple* p);

Hope that helps, friz
Still Learning...
frizb:
Right, that''s essentially what I''m doing right now. I just thought that there MUST be some nifty way to avoid that. I guess such a method doesn''t exist, eh?

-> Briar LoDeran <-
Or you can have the different functions for zebra in the actual class. The only problem with this is you probably want to use sword from another class also right? I think inheritance could somehow come into this, but I haven''t done much on it.
-------------I am a Juggalo.
I assume that each class you create will want to use the
Sword() function but each may implement it differently.

As Verminaard said, inheritence may be useful.

Create an abstract class:

class FuncClass {
public:
int Sword( void ) = 0;
};

then inherit this class into your Pineapple class and implement the Sword function:

class Pineapple : public FuncClass {
public:
int X;
int Y;
int Add( void ) { return (X + Y); }

int Sword( void ) { return (X - Y); }
};

Now in your code create a pointer of class FuncClass and point that at variable of class Pineapple:

Pineapple c;
FuncClass* ptr = &c

and then class the Sword function( ptr->Sword(); ).

You could also create another class which may implement the Sword() function differently but the same interface could be used:

class Apple : public FuncClass {
public:
int Color;

int Sword( void ) { return Color; }
};

...
Pineapple c;
Apple b;
FuncClass* ptr;
...

Now which ever class, c or b, you point *ptr at you can call the Sword() function. This will provide you with one interface but different implementations. It saves you having to create SwordPineapple( void ) and SwordApple( void ).

If you look in any C++ book under Polymorphism you will get a better understanding.

One last thing. You could implement the basic functionality into FuncClass::Sword() but you will also need to move variables X and Y into the same class.

Michael
gavcc:
I''ve reread your post a number of times and I''m not sure that I follow. I will try to state what I want more clearly so hopefully I can come to understand this better.

Hopefully this will be more clear:
Pineapple A(10);// Then we define:int Function1(void){  return X * Y;}int Function2(void){  return (X * X) + (Y * Y);}int Function3(void){  return X / Y;}...//  And then for each of the A(x)''s, I want to be able to //  do something like this:A(0).Zebra = Function1;A(1).Zebra = Function2;A(2).Zebra = Function3;...// and then:for(int i = 0; (i less than 10); i++){  cout << A(i).Zebra();} 


This is it...nothing that fancy. Is doing this possible? I don''t really want to have to define lots and lots of different classes. It seems I will just have to live with passing a pointer to get access to the class variables. But, I hope this made it clearer incase there is an answer!

Thank,
-> Briar LoDeran <-
Is the X and Y variable going to be common to many of the classes you create.

The way you described in the last post can't be done unless to reference can be passed to the function because it breaks the concept of encapsulation.

Now if X and Y are common try this:

class BaseData {  public:    int x, y;};class Pineapple : public BaseData {  public:    int (*Zebra)(BaseData* );    // Pass BaseData* so the function it points to has some way to reference the data};int Function_1(BaseData* ptr){  return ptr->x - ptr->y; }int Function_2(BaseData* ptr){  return ptr->x + ptr->y;}int Function_3(BaseData* ptr){  return ptr->x / ptr->y;}void main( void ){  Pineapple List(???);  // Assign int (*Zebra)(BaseData* ) to functions ...  p1.Zebra = Function_1;  p2.Zebra = Function_2;  p3.Zebra = Function_3;  ...  // Assign some data to list  ...  for(int i = 0; i < ???; i++) {    List.Zebra((BaseData *)&List(i));  }} 


Any common data needed by these functions Function_1, etc must should be added to the BaseData class so they have some way to access it.

Outside functions can not access data unless they have a reference.

Michael

Edited by - gavcc on June 15, 2000 1:30:55 PM
gavcc:
Thanks! This finally makes sense! All you UK guys seem to know your stuff.

-> Briar LoDeran <-
don''t bite your nails
everyone i talk to in the UK knows squat (about anything) - there are about 7 smart brits!

but thats just me being 'patriotic'



Edited by - POINT on June 15, 2000 6:50:23 PM

This topic is closed to new replies.

Advertisement