Set and Get value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Donos

    #1

    Set and Get value

    class CValue
    {
    public:
    void set(int n1) {m_i[0] = n1;};
    void get(int& n1) {n1 = m_i[0]};
    protected:
    int m_i[2];
    };

    class CSetValue
    {
    public:
    void CSetValue::Send Value()
    {
    CValue m_Value;
    int i = 10;
    m_Value.set(i);
    }
    };

    class CGetValue
    {
    public:
    void CGetValue::Reci eveValue()
    {
    int k;
    CValue m_Value;
    m_Value.get(k);
    }
    };

    I am expecting in class CGetValue, the integer variable "k", should
    get the value which was earlier set by "set" function.

    But somehow this is not working.

    Any idea?

    Thanks

  • Victor Bazarov

    #2
    Re: Set and Get value

    Donos wrote:
    class CValue
    {
    public:
    void set(int n1) {m_i[0] = n1;};
    void get(int& n1) {n1 = m_i[0]};
    Remove the semicolons after the function bodies.
    protected:
    int m_i[2];
    };
    >
    class CSetValue
    {
    public:
    void CSetValue::Send Value()
    {
    CValue m_Value;
    ^^^^^^^^^^^^^^^
    That's a local object. It exists only while this function is
    executing.
    int i = 10;
    m_Value.set(i);
    }
    };
    >
    class CGetValue
    {
    public:
    void CGetValue::Reci eveValue()
    {
    int k;
    CValue m_Value;
    ^^^^^^^^^^^^^^^
    That's *another* local object. It exists only when this function
    is executing. There is no connection between this object and
    the object with the same name in 'CSetValue::Sen dValue'.
    m_Value.get(k);
    }
    };
    >
    I am expecting in class CGetValue, the integer variable "k", should
    get the value which was earlier set by "set" function.
    >
    But somehow this is not working.
    >
    Any idea?
    Two unrelated objects in two unrelated functions. Setting the value
    of one does not affect the value of the other.

    You need to get out of OO (for now) and simply write

    void foo() {
    int a;
    a = 5;
    }

    #include <iostream>

    void bar() {
    int a;
    std::cout << a;
    }

    int main() {
    foo();
    bar();
    }

    Do you expect it to print out '5'? If yes, why? If not, why not?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Jim Langston

      #3
      Re: Set and Get value


      "Donos" <donguy76@gmail .comwrote in message
      news:1192812070 .220634.228790@ t8g2000prg.goog legroups.com...
      class CValue
      {
      public:
      void set(int n1) {m_i[0] = n1;};
      void get(int& n1) {n1 = m_i[0]};
      protected:
      int m_i[2];
      };
      >
      class CSetValue
      {
      public:
      void CSetValue::Send Value()
      {
      CValue m_Value;
      m_Value is a local variable to SendValue.
      int i = 10;
      i is a local variable to SendValue
      m_Value.set(i);
      The local variable m_Value has it's set method called, so the local variable
      m_Value has it's m_i[0] set to 10.
      }
      The function is over, so m_Value and i are destroyed. They don't exist
      anymore.
      };
      >
      class CGetValue
      {
      public:
      void CGetValue::Reci eveValue()
      {
      int k;
      k is a local variable to the RecieveValue function
      CValue m_Value;
      m_Value is a local variable to the RecieveValue function. It is default
      constructed (since the constructor is not initializing the array m_i it can
      contain any values).
      m_Value.get(k);
      The local variable m_Value has it's get function called, which loads the
      [any value] in m_i into k.
      }
      The function is over, m_Value and k are destroyed.
      };
      >
      I am expecting in class CGetValue, the integer variable "k", should
      get the value which was earlier set by "set" function.
      To get the same varible that was set, you'll need to operate .get on the
      same instance of the class as the .set.

      Unless you want all isntances of CValue to have the same m_i array, in which
      case declare it as static. Then there is just one m_i variable for all
      instances of CValue.

      Every time you do
      CValue SomeVariableNam e;
      you create a NEW instance of CValue with it's own m_i variable.


      Comment

      • Andrej Hristoliubov

        #4
        Re: Set and Get value

        class CValue
        {
        public:
        void set(int n1) {m_i[0] = n1;}
        void get(int& n1) {n1 = m_i[0]}
        protected:
        int m_i[2];
        };

        class CSetValue
        {
        public:
        void SendValue()
        {
        // CValue m_Value;
        int i = 10;
        m_Value.set(i);
        }
        };

        class CGetValue
        {
        public:
        void RecieveValue()
        {
        int k;
        // CValue m_Value;
        m_Value.get(k);
        }
        };

        CValue m_Value;

        void main()
        {
        CSetValue SetValue;
        SetValue.SendVa lue();
        CGetValue GetValue;
        GetValue.Reciev eValue();
        }

        "Donos" <donguy76@gmail .com???????/???????? ? ???????? ?????????:
        news:1192812070 .220634.228790@ t8g2000prg.goog legroups.com...
        class CValue
        {
        public:
        void set(int n1) {m_i[0] = n1;};
        void get(int& n1) {n1 = m_i[0]};
        protected:
        int m_i[2];
        };
        >
        class CSetValue
        {
        public:
        void CSetValue::Send Value()
        {
        CValue m_Value;
        int i = 10;
        m_Value.set(i);
        }
        };
        >
        class CGetValue
        {
        public:
        void CGetValue::Reci eveValue()
        {
        int k;
        CValue m_Value;
        m_Value.get(k);
        }
        };
        >
        I am expecting in class CGetValue, the integer variable "k", should
        get the value which was earlier set by "set" function.
        >
        But somehow this is not working.
        >
        Any idea?
        >
        Thanks
        >

        Comment

        • Old Wolf

          #5
          Re: Set and Get value

          On Oct 20, 6:10 am, "Andrej Hristoliubov" <a...@mail.ruwr ote:
          class CSetValue
          {
          public:
          void SendValue()
          {
          // CValue m_Value;
          int i = 10;
          m_Value.set(i);
          This is an error, the identifier m_Value is undeclared.

          You have to put your declaration of m_Value before the
          definition of CSetValue.

          Comment

          Working...