如何在两个不同的类(或.cpp)之间共享变量 [英] How to share variables between two different classes (or .cpp)

查看:57
本文介绍了如何在两个不同的类(或.cpp)之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,两个类在一个项目中。(A.cpp和B.cpp)



我想创建一个由上面共享的变量X两个班。



我在A.cpp中生成X并在Ah中声明,接下来我在B.cpp的头部包含了Ah



但这会导致编译错误...



在B.cpp中,我无法知道X的值。



请告诉我方便的方式。



谢谢。



- = - = - = - = - = - = - = - = - = - = - = - = - = - = - 详细说明= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =





A.form(使用LinkCtrl报告样式)显示查询屏幕

------------------------ -------------------------------------------------- ---------------

头条(字段名称......)

---------- -------------------------------------------------- -----------------------------

记录1(显示数据信息)

记录2

记录3





如果双击一行屏幕,下一个显示B.form



B.form显示更新屏幕(字段信息来自上述记录之一)

--- -------------------------------------------------- -----------------------

字段1:值1

字段2:值2

字段3:值3

.....



任何人都可以修改value1,value2 ......

所以,B.form有相同的A.form形式记录信息



这可以理解吗?



我尝试了什么:



我有一些关于这些问题的代码,但我失败了。

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.

推荐答案

第一步可以将类 A 的指针(或引用)传递给类 B 。如果及时创建类 B (双击一行时),则可以将其传递给构造函数。否则实现某种设置功能:



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:

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

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



在类 B 源文件中包含类<$ c $的头文件c> A 并实现 setDataFromA 函数。



从类中获取数据 B 进入类 A ,在类 B 中实现get函数或者通过编写 A :: setDataFromB(const B * classB)函数来做类似的事情(这又需要向类 B 在类 A 头文件中。)


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).


这篇关于如何在两个不同的类(或.cpp)之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆