How to get store a value returned by a const member function?

Discussion in 'C++' started by rag84dec, Feb 6, 2008.

  1. rag84dec

    rag84dec New Member

    Joined:
    Jul 17, 2007
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Hi ,
    I have a const member function which returns a protected member of a class.Like this
    Code:
    class A
    {
    protected:
    int Number;
    public:
     int   Get_Number   (void) const   { return(Number);  }
    };
    
    

    I want to use the value returned by the Get_Number function.
    How can i do

    int temp=Data.Get_Number(); is giving me an execption(Data is of type "A&")


    Can anyone help me??...
     
  2. rvirma

    rvirma New Member

    Joined:
    Feb 6, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Data is object of class A . Declare it

    const A Data;
     
  3. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Code:
    class MyTest
    {
    protected:
      int n;
    public:
      int GetVal()const
      {
        return n;
      }
      void SetVal(int num)
      {
        n=num;
      }
    };
    
    int main()
    {
     int temp;
     MyTest obj;
     obj.SetVal(10);
     temp=obj.GetVal();
    cout<<temp;
     return 0;
    }
    
    This code has no problems. What's your problem??? Let me know exactly what u wanna?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice