Need acces to Class A->ClassB->one_function, how to declare ?

I have a Class A, with an instance of ClassB.: myclass_a_b
From the main :
1
2
ClassA myclassA;
  myclassA.myclass_a_b.accesible_method();


My class_a_b must to be public, if not I dont see it at 'main'myclassA.
But I only want to let access 'accesible_method();'


How can I do this ? I have a little confusion with public, protected, friend, etc..
Thanks

Last edited on
You can do it like so:

1
2
3
4
5
6
7
8
9
class ClassA
{
  ClassB myclass_a_b;
public:
  void call_myclass_a_b_accesible_method()
  {
    myclass_a_b.accesible_method();
  }
};
I think I dont explain very well
I'm asking for a possible way to call directly the function of class A, created at class B, and this last created at main.

I have :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ClassA (.h and cpp)
the_function_of_a_I_want_to_be_accesible


ClassB (.h and cpp)
#include "classA.h"  
  private  ClassA myclassA;
  public a_trick_function(){
     cmyclassA.the_function_of_a_I_want_to_be_accesible();
  }

Main
 ClassB myclassB;
  myclassB.a_trick_function(); 


Is there any other cheap way ?

Thanks



Maybe you're thinking of a friend class: http://cplusplus.com/doc/tutorial/inheritance/
Last edited on
class A {
public:
function_you_want(){
};

class B : public class A {

};


In Main you can then do :

B Variable;

Variable.function_you_want();

(As coder777 said look up inheritance.)
Last edited on
Topic archived. No new replies allowed.