access one classes function from another class

Mar 23, 2013 at 1:43pm
I just restarted programming (I had given up for a while), and have ran into a problem. How do you access the function that is in one class from another:
1
2
3
4
5
6
7
8
9
class a
{
public:
void afunc();
};
class b
{
void bfunc (a::afunc);//is that how you do this?
};

So that's what I want to do, but it doesn't work. Any help?
Last edited on Mar 23, 2013 at 2:11pm
Mar 23, 2013 at 2:13pm
In order to call function afunc();, you need an object of type a.

So that's what I want to do
I'm not sure what it is that you want to do.
Mar 23, 2013 at 2:21pm
I want to be able to have bfunc use afunc. I was just asking how to do that. So I would have to do this?
1
2
3
4
5
6
7
8
9
10
class A
{
public:
void Afunc();
};
class B
{
A a;
void Bfunc (a.A::afunc);
};

So like that?
Mar 23, 2013 at 2:28pm
I want to be able to have bfunc use afunc.

Can you expand on or explain the word "use" here.

Currently it seems that you'd like to pass the address of the function as an input parameter to a function of class b. But somehow I don't think that's what you want or need.
Mar 23, 2013 at 2:34pm
By use I just mean be able to do something like this:
1
2
3
4
Bfunc
{
cout<<Afunc;
}

I keep on trying to do this but it gives me errors.
Mar 23, 2013 at 2:42pm
Maybe like this,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A
{
public:
    int Afunc() { return 5; };
};

class B
{
    A a;
public:
    void Bfunc ()
    {
        cout << a.Afunc();
    };
};
Last edited on Mar 23, 2013 at 2:42pm
Mar 23, 2013 at 2:50pm
Oh thanks...I feal realy dumb
Topic archived. No new replies allowed.