C++:如何将变量的值从一个类传递到另一个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17991140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:38:47  来源:igfitidea点击:

c++: how to pass a variable's value from one class to another?

c++

提问by McLan

I have a class "A" that take its variables' values from the application's GUI. let us say one of these variables is "X". then, in another class "B", I would like to call this "X" and pass its value to another function.

我有一个“A”类,它从应用程序的 GUI 中获取其变量的值。让我们说这些变量之一是“X”。然后,在另一个类“B”中,我想调用这个“X”并将其值传递给另一个函数。

what I did is that I created an object of class "A" called "objectA" in class "B", and then called the value of "X" as follow:

我所做的是我在类“B”中创建了一个名为“objectA”的类“A”的对象,然后调用“X”的值如下:

// class A:
void classA::function1(int gui_value){
    x= gui_value;
}

then in the another class:

然后在另一个班级:

//class B:
void classB::run(){
.
.
function2(/* I need to pass x here */); 
.
.
}

both functions and x are defined in the header file correctly.

函数和 x 都在头文件中正确定义。

what I did is that I created an object of classA (let us say: objectA) in the header file and call the value of "x" in "function2" in "classB" using "objectA" as follow:

我所做的是在头文件中创建了一个 classA 的对象(让我们说:objectA),并使用“objectA”调用“classB”中“function2”中“x”的值,如下所示:

//class B:
void classB::run(){
.
.
function2(objectA->x); 
.
.
}

Let us say "x" value equals "5", when I debug the value of "x" in function2, it gives me random number everytime i debug. So, I am just guessing that these random values are already exist in the memory, which means the value of "X" is not passed correctly.

假设“x”值等于“5”,当我在 function2 中调试“x”的值时,每次调试时它都会给我随机数。所以,我只是猜测这些随机值已经存在于内存中,这意味着“X”的值没有正确传递。

I think, although the object is created, however it create a new instance of variable "x" everytime. i.e.: is not taking the value of "x" from classA, it is just create an object of classA with all its variables.

我认为,虽然创建了对象,但它每次都会创建一个新的变量“x”实例。即:不是从 classA 中获取“x”的值,它只是创建一个 classA 的对象及其所有变量。

my problem is how to pass the vlaue of "x" that is obtained in classA directly to classB without creating a new instance of its variable ?

我的问题是如何将在 classA 中获得的“x”的 vlaue 直接传递给 classB 而不创建其变量的新实例?



Update:

更新:

Thanks to doctorlove'sanswer, my code is now working fine. My problem was in initializing "objectA" after its creation and assigning a value to it. what I was doing is creating the object and passing it directly in "classB". That is why i was getting the random values as it was just a new object which have whatever value in the memory in the time of creation.

感谢医生的回答,我的代码现在可以正常工作了。我的问题是在创建后初始化“objectA”并为其分配一个值。我正在做的是创建对象并将其直接传递到“classB”中。这就是为什么我得到随机值的原因,因为它只是一个新对象,在创建时在内存中具有任何值。

采纳答案by doctorlove

It is not clear what you are trying to do. If you have something like this...

目前尚不清楚您要做什么。如果你有这样的事情...

class A
{
    private:
    int X;//ignoring any thing about encapsulation for now
};

class B
{
//...
    public:
    void DoSomething()
    {
        A objectA; 
        //makes objectA another A each time we get here, with X uninitialised
        //use objectA.X which has random numbers in
    }

};

this will behave similarly to the problem you describe. If you make objectAa member instead and initialise it properly it might do what you want. With class Aas before,

这将与您描述的问题类似。如果你objectA改为创建一个成员并正确初始化它,它可能会做你想要的。与class A以前一样,

class B
{
//...
    private:
    A objectA;
    public:
    B() 
    {
        objectA.X = 42;
    }
    void DoSomething()
    {
        //use member objectA.X 
    }

};


EDITClearly in my example I have pulled 42 out of nowhere. If you have a sensible value from the gui, send it to the constructor of B. If it changes you'll need a reference to it instead.

编辑 很明显,在我的例子中,我不知从哪里拉出了 42。如果您从 gui 中获得了合理的值,请将其发送给 B 的构造函数。如果它发生变化,您将需要对其进行引用。

explicit B(int gui_value)
{
    objectA.X = gui_value;
}


EDIT2If you have an existing objectAsomeplace else, then pass thatto the constructor

EDIT2如果您有一个现有objectA别的地方,然后通过给构造

explicit B(const A & otherA)
    : objectA(otherA)
{
}

If that's not possible, your design needs improving. Without any code to look at I cannot help any more.

如果这是不可能的,您的设计需要改进。没有任何代码可以查看,我无能为力了。

回答by Engine

let's say you got a class A with an private int variable i , and B with and private int variable X,

假设您有一个带有私有 int 变量 i 的类 A 和带有私有 int 变量 X 的 B 类,

class A{
private:
int i;  
// if you want to use x within A you have to declare the  object here 
B  objectB;
.....
public :
int getI();
int setI();
};

// the second class  B 
class B(){
private :
int X;
......
public :
int getX();
void setX();
..

};

let'S say you have a main() function

假设你有一个 main() 函数

int main (){
  A objA;
  B objB;

  objB.setX(5);//X is now 5 
  objA.setI(objB.getx());  // i has the value of X which is 5 
...
}

回答by Programmer

Since there is no compile time error on the creation of the objectA, the object is successfully been created.

由于在创建 时没有编译时错误objectA,因此成功创建了对象。

To pass the variable, instead of using arrow operator use dot(.) operator y = objectA.X

要传递变量,而不是使用箭头运算符,请使用 dot(.) 运算符 y = objectA.X