Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
For example, Two classes are within one project.(A.cpp and B.cpp)

I want to make one variable X which is shared by above two classes.

I generated X in the A.cpp and declared in the A.h, next I included "A.h" in the head of B.cpp

But it makes compile error...

In the B.cpp, I cannot know the value of X.

Please let me know the convenient ways.

Thank you.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Description In detail =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


A.form (use LinkCtrl report style) show inquiry screen
-----------------------------------------------------------------------------------------
Head Line (Field names...)
-----------------------------------------------------------------------------------------
records 1 (display informations of data)
records 2
records 3
.
.
if one double click one row of screen, next display B.form

B.form show update screen(field info is derived from one of above records)
----------------------------------------------------------------------------
field 1 : value 1
field 2 : value 2
field 3 : value 3
.....

Anyone can modify value1, value2 ...
So, B.form has same informations of clicked record of A.form

Is this possible to understand?

What I have tried:

I have some codes about that problems, but I failed.
Posted
Updated 16-Mar-16 4:43am
v4
Comments
Jochen Arndt 16-Mar-16 10:06am    
There is no simple solution because there may be multiple instances of each class (even instances of the same class did not know about the content of members of another instance).

You should describe what you finally want to achieve (use the green 'Improve question' link). Then you may get a solution that solves your problem.

1 solution

The first step can be passing a pointer (or reference) of class A to class B. If class B is created in-time (when double clicking on a row), it can be passed to the constructor. Otherwise implement some kind of set function:

C++
// Class B header
// Forward declaration to class A
class A;

class B
{
    B();
    //B(const A *classA);
    // ...
    setDataFromA(const A *classA);
};

In the class B source file include the header file of class A and implement the setDataFromA function.

To get data from class B into class A, implement get functions in class B or do it similar by writing a A::setDataFromB(const B *classB) function (which again requires a forward declaration to class B in the class A header file).
 
Share this answer
 

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