CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2005
    Location
    BANGALORE ,INDIA
    Posts
    19

    Question Calling a function of another class.

    Hi,

    There are two classes A and B.

    class A
    {

    void a1(char )

    }

    and

    class B
    {

    void b1(char )

    }

    Now, i need to call a1 function from b1.How this can be done.

    Regards,
    harsha.

  2. #2
    Join Date
    Jan 2004
    Posts
    206

    Re: Calling a function of another class.

    A simple illustration :

    Code:
    class A
    {
    public:
    void a1(char );
    };
    
    class B
    {
    public:
    void b1(char x)
    {
    A AInstance;
    AInstance.a1(x);
    }
    };

  3. #3
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Calling a function of another class.

    some thoughts. There are many possibilities here and it really depends on what you are doing

    1. The class A has nonstatic state and the function a1 uses this state.
    the state A holds includes more than the parameters of b1 provide or has scope beyond b1 and or the instantiation of B.
    This would imply that you can not just instantiate A inside b1 as indicated. Your options are to
    a) have a globally accessable instance of A, such as a singleton instance.
    b) pass and instance of A into b1 as a paramater, or into B's constructor and hold the instance of A as a member of B.

    2. The function a1 uses no non-static state information from A.
    a) Make a1 a static member fucntion of A.
    b) a1 should not be a member of A at all.

    3. a1 only needs to have scope the body of b1 and can be fully instantiated with the parameters of b1
    a) instantiate a local instance of A, as indicated by the previous post.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Calling a function of another class.

    Quote Originally Posted by harshandu
    Now, i need to call a1 function from b1.How this can be done.
    What is the relationship between these two classes?

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Calling a function of another class.

    In the current form, you cannot! The only way you can make the call is making the class B a friend of A. Otherwise, the member functions should be public and you call the function using the object of A in B's function. Hope this helps. Regards.

    *** Wish you all a very happy new year 2006 ***

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured