McLan
McLan

Reputation: 2678

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

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.

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:

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

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:

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

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.

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 ?


Update:

Thanks to doctorlove's answer, 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.

Upvotes: 1

Views: 27299

Answers (3)

Engine
Engine

Reputation: 5420

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

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

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

Upvotes: 1

doctorlove
doctorlove

Reputation: 19232

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 objectA a member instead and initialise it properly it might do what you want. With class A as before,

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

};

EDIT Clearly 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.

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

EDIT2 If you have an existing objectA someplace else, then pass that to the constructor

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.

Upvotes: 1

Programmer
Programmer

Reputation: 475

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

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

Upvotes: 0

Related Questions