|
-
December 31st, 2005, 01:23 AM
#1
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.
-
December 31st, 2005, 02:37 AM
#2
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);
}
};
-
December 31st, 2005, 04:43 AM
#3
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."
-
December 31st, 2005, 06:01 AM
#4
Re: Calling a function of another class.
 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?
-
December 31st, 2005, 09:46 AM
#5
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 ***
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|