Ramilol
Ramilol

Reputation: 3462

How to access variables in different class from other class?

Let just say that we have two classes, A and B.
Here is code for both of them

class A
{
public:
    int x;
};

class B
{
public:
    int y;
    void FindY() { y = x + 12; }
};

void something()
{
    A fs;
    B fd;
    fs.x = 10;
    fd.FindY();
}

the problem is that i want to access x but i don't wanna pass anything as argument to my function i look at friend and inheritance but both didn't seem to help me, correct me if i'm wrong.
some how i need to find x in function FindY().
I'm going with the static method but in my case i get this error.

Error 2 error LNK2001: unresolved external symbol "public: static class std::vector<class GUIDialog *,class std::allocator<class GUIDialog *> > Window::SubMenu" (?SubMenu@Window@@2V?$vector@PAVGUIDialog@@V?$allocator@PAVGUIDialog@@@std@@@std@@A) C:\Users\Owner\documents\visual studio 2010\Projects\Monopoly\Monopoly\Window.obj Here is how i declared it

static vector<GUIDialog *> SubMenu;

I get that error because of this line

SubMenu.resize(3);

Upvotes: 2

Views: 67338

Answers (3)

villintehaspam
villintehaspam

Reputation: 8734

Three different approaches:

  1. Make B::FindY take an A object as a parameter

    class B {
    public:
      void FindY(const A &a) { y = a.x + 12; }
    };
    
  2. Make A::x static

    class A {
    public:
      static int x;
    };
    class B {
    public:
      void FindY() { y = A::x + 12; }
    };
    
  3. Make B inherit from A.

    class B : public A {
    public:
      void FindY() { y = x + 12; }
    };
    

CashCow also points out more ways to do this in his answer.

Upvotes: 8

CashCow
CashCow

Reputation: 31445

Assuming that A is correct and you cannot change it, i.e. x is a member variable, you will need an instance of an A in order to use its x member.

Thus said we can modify B but you need FindY() to take no parameters.

Therefore we need to bring in the A with an earlier call and store it as a class member.

class B
{
public:
   A a;
   int y;
   void FindY() { y = a.x + 12; }
};

This is just an outline. This is what is commonly done for "functor" classes where the function is operator() and takes a fixed number of expected parameters but we want more. The whole of boost::bind is based on this principle.

Upvotes: 1

Frerich Raabe
Frerich Raabe

Reputation: 94539

As it is, x is not a variable of the class A but a variable of objects ("instances") of class A. There are at least two ways to make x accessible from B::findY without passing anything to the function:

  • Instantiate an object of class A inside the B::findY function:
    class B
    {
    public:
        int y;
        void FindY() { A a; y = a.x + 12; }
    };
  • Make x a static variable, so that it's a variable on the class itself. You don't need to instantiate objects in this case, but the variable will be common for all objects of type A (so you cannot have different values of x for different objects):
    class A
    {
    public:
        static int x;
    };

    class B
    {
    public:
        int y;
        void FindY() { y = A::x + 12; }
    };

Upvotes: 1

Related Questions