Skip to main content
gamedev.net
🔒 Locked

Using Function Pointers to access Class members

Started by giant Apr 22, 2002 at 4:19 AM 6 replies 350+ views
Original Post
giant
giant
Hi. I asked some basic questions here on Function Pointers, a few days ago, and got some very good answers, so i was hoping that ye could help me with the following. I have 2 classes ( called classA and classB [ cool names ehhh] ) A is defined first, then B. Considering B is defined after A, it is possible for B to have an instance of A as a member, but not possible for A to have an instance of B as a member. My problem is that I want to be able call a m,ember function of B from inside a member function of class A. I thought that the best way to do this would be to pass a function pointer of the function
  
class classA
{
public:
    void CallsBFromHere( void (*Funct)() ) { Funct(); }
};

class classB
{
public:
    void CallThis( ) ( cout<<"Go in here from A"; )
    void (*Funct)();
private:
    classA A;
};

int main()
{
    classB B;
    B.Funct = CallThis;
    B.A.CallsBFromHere( Funct );
    return 0;
}
  
it was something like that I was trying, but it wouldnt work. Does anyone have any ideas what I can do to get this to work. Giant "Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." --Albert Einstein
Bashar
Bashar
why not pass a pointer to instance of B to A''s function, then call B''s function?

void A::fooA( class B * pBClass ) {
pBClass->fooB();
}
People fear what they don''t understand, hate what they can''t conquer!
giant
giant
Hi.
Thanks for your reply. You suggestion however will not work.
If I do the following


  
class classA
{
public:
void CallsBFromHere( classB *pBClass}
};

class classB
{
public:
void CallThis( ) ( cout<<"Go in here from A"; )
classA A;
};



it will cause an error, cause I am using a pointer to classB before classB is even defined, and it will not know what it is.

Giant

"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." --Albert Einstein
Dactylos
Dactylos
quote:

Original post by giant
I am using a pointer to classB before classB is even defined, and it will not know what it is


You can forward declare class B before the definition of A, i.e.

class B;

class A { ... };

class B { ... };

this way you can declare pointers or references to B in A, since the compiler knows that a class B will eventually be defined.

[edited by - Dactylos on April 22, 2002 5:49:15 AM]
giant
giant
Hi Dactylos.

I am trying to implement what you are suggesting, but am getting some errors.

class.cpp: In method void A::Funct(B *)'':
class.cpp:8: invalid use of undefined type
class B''
class.cpp:3: forward declaration of `class B''

Here is the source


  
#include<iostream.h>

class B;

class A
{
public:
void Funct( class B* classptr ) { cout<<"Hello\n"; classptr->Call(); }
};

class B
{
public:
A instA;
void Call() { cout<<"\nInside A"; }
};

int main()
{
B instB;
return 0;
}


Thanks for your help.

Giant

"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." --Albert Einstein
Dactylos
Dactylos
You must define class B before defining A::Funct, since B is used in it. Try something like this (modified your code):

      
#include<iostream.h>

class B;

class A
{
public:
void Funct( class B* classptr ); // don't define the body here

};

class B
{
public:
A instA;
void Call() { cout<<"\nInside A"; }
};

// Here B has been defined, and it is therefore legal to use objects of type B.

void A::Funct(B * classptr)
{
cout<<"Hello\n";
classptr->Call();
}

int main()
{
B instB;
return 0;
}


[edited by - Dactylos on April 22, 2002 6:06:30 AM]
Dactylos
Dactylos
Regarding your original question:
quote:
Original post by giant
My problem is that I want to be able call a m,ember function of B from inside a member function of class A.

I thought that the best way to do this would be to pass a function pointer of the function


Personally I don''t see how this is a problem, as long as you don''t define all your function bodies inside the classes (i.e. define the function-bodies after both classes have been declared).

A can not however contain an object of type B if A is declared before B. Either reverse the order of declaration or store a pointer to B in A (if you need to).
burp
burp
You could also use polymorphism.


  
class X
{
public:
virtual void CallThis()=0;
};

class A: public X
{
public:
void CallThis() {};
void CallsBFromHere(X *pX) { pX->CallThis(); }
};

class B: public X
{
public:
void CallThis() { cout << "Go in here from A" << endl; };
A a;
};

void main(void)
{
A a;
B b;

a.CallsBFromHere(&a);
a.CallsBFromHere(&b);
b.CallThis();
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.