none
How to copy the value from one class to another in C++? RRS feed

  • Question

  • // sample.h class A{ public: A(); void fun(); }; class B{ public: B(); void fun2(); }; ///////////////////////////////////// // A.cpp #include "sample.h" void A::fun() { int x =10; } //////////////////////////////////// // B.cpp #include "sample.h" void B::fun2() { int y = x; // how can I get the value of x as 10 here which is initialized in A.cpp? }

    // I do not want to make the variable global. I there any other solution? please suggest.


    Thanks & Regards, Mayank Agarwal

    Friday, March 7, 2014 7:20 AM

Answers

  • You can't, that x variable is local to the A::fun function.

    The question you should ask is more like "how do I organize my data in such a way that I don't end up needing to access the value of a local variable from another function". And the answer to that is: well, it depends what you are trying to do.

    Friday, March 7, 2014 7:41 AM
    Moderator
  • "the value which is stored in variable "x" of class A variable"

    OK, so you are saying that x is in fact supposed to be a member of class A and not a local variable. Well, next time try to post a more realistic example.

    Now, if x is indeed such a member then the only way for B::fun2 to access it is via a reference/pointer to an object of class A. Perhaps sent as a parameter to B::fun2:

    // sample.h
    class A {
    public:
        int x;
        A();
        void fun();
    };
    class B {
    public:
        B();
        void fun2(A *);
    };
    
    // A.cpp
    #include "sample.h"
    void A::fun() {
        x = 10;
    }
    
    // B.cpp
    #include "sample.h"
    void B::fun2(A *a) {
        int y = a->x;
    }
    
    You may consider adding a getX function to A and use that instead of accessing x directly. But one way or another B::fun2 has to get its hands on a object of class A in order to access the value of x.

    Friday, March 7, 2014 8:08 AM
    Moderator

All replies

  • You can't, that x variable is local to the A::fun function.

    The question you should ask is more like "how do I organize my data in such a way that I don't end up needing to access the value of a local variable from another function". And the answer to that is: well, it depends what you are trying to do.

    Friday, March 7, 2014 7:41 AM
    Moderator
  • Yes I mean the same.  Actually B.cpp is a class which is responsible to write data in a file after few calculations. And A.cpp is the class where the data is received from the user.

    the value which is stored in variable "x" of class A variable , I need to take that to Class B.

    What can be the best approach to implement this kind of problem?

    Thanks in Advance. 


    Thanks & Regards, Mayank Agarwal

    Friday, March 7, 2014 7:55 AM
  • "the value which is stored in variable "x" of class A variable"

    OK, so you are saying that x is in fact supposed to be a member of class A and not a local variable. Well, next time try to post a more realistic example.

    Now, if x is indeed such a member then the only way for B::fun2 to access it is via a reference/pointer to an object of class A. Perhaps sent as a parameter to B::fun2:

    // sample.h
    class A {
    public:
        int x;
        A();
        void fun();
    };
    class B {
    public:
        B();
        void fun2(A *);
    };
    
    // A.cpp
    #include "sample.h"
    void A::fun() {
        x = 10;
    }
    
    // B.cpp
    #include "sample.h"
    void B::fun2(A *a) {
        int y = a->x;
    }
    
    You may consider adding a getX function to A and use that instead of accessing x directly. But one way or another B::fun2 has to get its hands on a object of class A in order to access the value of x.

    Friday, March 7, 2014 8:08 AM
    Moderator