Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
A class(A.cpp) has a unique function:

class A
{
public:
void m_function(int* x);
}

1) Another B class(B.cpp) want to use the m_function by transferring the value of 'x';

2) In the assumption, B class does not the variable y,z)

void m_function(int x)
{
int y;
int z = x + y;
}

How do I code the program inside the B.cpp?

What I have tried:

For one day or longer, I wasted to code above it.
Posted
Updated 22-Mar-16 17:16pm
Comments
Sergey Alexandrovich Kryukov 22-Mar-16 23:14pm    
In such things, answers hardly can help much. You need to learn the very basics of programming, which you are simply unaware of. A study on regular basis will quickly fix it.

Here is a hint for you: with functions you are showing, you work not with classes, but with class instances, objects. Can you spot the difference? Such functions are called instance functions, and others are static functions. This is what you have to understand.

—SA

1 solution

Please see my comment to the question and also my past answer: Catch 22 - Pointers to interface objects die when function using them is made static.[^].

This answer is about C#, not C++ (sorry about it), but you can understand it, because the essence of thins is exactly the same: C# windows base this key word related and its uses in the application[^].

In other words, you need an instance of a class to call its instance function. Let's consider a call of a function with one argument:
C++
myInstance.myFunction(parameterValue);

If you have a pointer to an instance, '.' operator simply becomes '->'.
In this schema, myInstance is passed as the first (implicit) argument and becomes "this" pointer inside a function, when it it called, and parameterValue (actual parameter) comes next, and so on.

Where to get myInstance? Ultimately, it should be returned by some constructor call. And so on… It would chain, question by question, to whole programming field, but it would make a little sense to try to answer all such question. Regular study, this is the only thing which helps.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900