Calling function from 'owner' class

Feb 9, 2011 at 12:13am
Hello,

I'm having trouble figuring out how to call a function from inside a class to a class which holds the object.
It's a bit hard for me to explain, because I'm mixing up the different terminologies, so here's an example of what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class A
{
	A();
	B *objectB;
	void Function01 (int a);
}

class B
{
	B();
	void Function02 (int a); //
}

A::A()
{
	objectB = new B();
}

A::Function01(int a)
{
	//Do things
}

B::B()
{
	//.....
}

B::Function02 (int a)
{
	//Call to Function01
}


In the above example how would I call Function01 from function02?
Thanks in advance!
Last edited on Feb 9, 2011 at 12:14am
Feb 9, 2011 at 12:22am
There are many ways you can do this, here is one example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class A
{
   A()
   {
   };
    int Function1()
    {
      //Do something
    }
}

class B
{
private:
    A *objectA ;
public:
    B()
    {
           objectA = new A();
     }
     void Function2()
     {
           objectA->Function1();
      }
}



but there are other ways such as friend functions and friend classes, I encourage you to look at those.
Last edited on Feb 9, 2011 at 12:29am
Feb 9, 2011 at 12:54am
This is generally something that should be avoided, since B shouldn't assume it's owned by an A (ideally, B would know nothing about A)

If it's unavoidable, the best way would probably be to give B a pointer to it's owner:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class A
{
  B* b;
  void func_a();

  A();

  //..
};

class B
{
  A* owner;

  void func_b()
  {
    owner->func_a();
  }

  B(A* own) : owner(own)
  {}
};


A::A()
{
  b = new B(this);  // this is the owner
}


But again -- avoid if you can. Ask yourself: "Does B really need to know about A?"
Feb 9, 2011 at 12:57am
Thanks for the response!
I did try the method you described, but somehow the compiler says that the class name doesn't name a type (even though I included the class header file). I'll take a look at friend classes and friend functions next.
Feb 9, 2011 at 1:03am
Strange - I did get this to work....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

class A
{
public:
   A()
   {
   };

    int Function1()
    {
      //Do something
		cout << "Hello";
		return 1;
    }
};

class B
{
private:
    A *objectA ;
public:
    B()
    {
           objectA = new A();
     }
     void Function2()
     {
           objectA->Function1();
      }
};

int main()
{
	B *objectB = new B();
	objectB->Function2();
	return 0;
}


Feb 9, 2011 at 1:40am
I did try the method you described, but somehow the compiler says that the class name doesn't name a type (even though I included the class header file). I'll take a look at friend classes and friend functions next.


Friends won't help you. Friends just let you get around private/protected access issues. That's not your problem here.

You shouldn't be including carelessly. Instead you should be forward declaring:

1
2
3
4
5
6
7
8
9
10
11
12
// a.h
#ifndef A_H_INCLUDED
#define A_H_INCLUDED

class B;  // forward declare

class A
{
  // ...
};

#endif // A_H_INCLUDED 

1
2
3
4
5
6
// a.cpp

#include "a.h"  // include them both here
#include "b.h"

//... 


b.h and b.cpp should look similar.

See this article for more details:

http://www.cplusplus.com/forum/articles/10627/#msg49679
Feb 9, 2011 at 2:00am
Thanks for the replies!
I'll try forward declaring tomorrow, it's 3 am for me, time for some sleep...
Feb 9, 2011 at 2:18am
Sorry, I couldn't resist. But when I saw the brief excerpt "I'll try forward declaring tomorrow..." I immediately thought of

1
2
3
4
5
6
// Forward declare Tomorrow
class Tomorrow;

class Today {
    Tomorrow* nextDay;
};


Bad programmer joke.

Anyway.
Feb 9, 2011 at 2:38am
closed account (zwA4jE8b)
I don't get it?
Feb 9, 2011 at 3:08am
lol jsmith
Feb 9, 2011 at 10:53am
:) LOL
Feb 9, 2011 at 11:13am
I found the solution to my problem. jsmiths post hinted me that it had something to do with how I included the different headers, so first I tried to include the 'B' class in A's .cpp file instead of in its header. This did the trick.
Forward declaring would not have worked in my case I think, because I used functions of 'B' in 'A'.
Thanks for all the replies!
Topic archived. No new replies allowed.