Thread: call methods across classes

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    class A
    {
    public:
         void foo()
         {
    
         }
    };
    
    class B
    {
    public:
         void bar( A &a_inst )
         {
            a_inst.foo();
         }
    };
    
    /* ... */
    
    int main( void ) {
      A a;
      B b;
    
      b.bar( a );
    
      return 0;
    }
    But foo has to be a public function unless you want to declare B as a friend class within A:

    Code:
    class A {
    friend class B;
    
    private:
      void foo() {
        std::cout<< "A::foo() method called\n";
      }
    
    public:
    
    };
    
    class B
    {
    private:
    
    
    public:
      void bar( A &a_inst ) {
        a_inst.foo();
      }
    };
    
    /* ... */
    
    int main( void ) {
      A a;
      B b;
    
      b.bar( a );
    
      return 0;
    }
    Last edited by twomers; 09-11-2007 at 08:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  4. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM