Nested Class using function in outer class?

class A:
hello()
greeting()
<>class B:
<>hello()

is there a way so that class B can access the functions in class A without having to retype the function?
What do you mean by "retype"?
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
// my first program in C++
#include <iostream>

class Foo {
  void hello() const {
    std::cout << "Hello Dolly!\n";
  }

public:
  struct Bar {
    void hello(int x) const {
      std::cout << "Hello " << x << " World!\n";
    }
    void hello( const Foo& f ) const {
      f.hello();
    }
  };
};

int main()
{
  Foo::Bar a;
  a.hello( 1 );
  Foo b;
  a.hello( b );
}

Hello 1 World!
Hello Dolly!
Nested classes can access all member functions (and member variables) of the outer class but if it's a non-static member function you still need an object to call it on (as always).
Last edited on
Topic archived. No new replies allowed.